How to detect input voltage change on specific pin of atmega32? DC jack sensing
What you are looking for is external interrupts. The chip that you are useing has hardware that can cause an interrupt if there is a logical change on some dedicated pins. Check out the documentation on page 66 about external interrupts. It looks like INT0 INT1 and INT2 can react to external interrupts if set (pins PD2, PD3 and PB2). You could be polling the PC7, but it's inefficient.

Basically, you need to utilize external interrupts futures of the chip, which are limited to certain pins. You write a interrupt subroutine and when interrupt happens, the subroutine is executed.

Also, check p.287 Voltage on any Pin except RESET with respect to Ground -0.5V to VCC+0.5V, thus you can not provide 9V to it.