Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* function to read value from one port, and control the led blinking on another
- port by transferring data of the read port to the controlling port
- data read from port B, and led controlled from port D */
- // ATmega8, clock freq=1 MHz.
- #include <avr/io.h>
- #include<util/delay.h>
- #include<math.h>
- int main(void) {
- unsigned int polarity=0;
- unsigned int speed=0;
- double speed_n;
- int i;
- DDRB=0x00; //port B set as input
- DDRD=0xFF; //port D set as output
- PORTD=PINB;//port B input transferred to port D, PINB is register storing input data for port B
- while(1)
- {
- // polarity will be 0 or 1 based on whether B7 is low or high
- PINB>127?(polarity=1):(polarity=0); // for PINB to be > 127, B7 must be high
- speed=(PINB)&(0x7F); //masking B7, thus B0 to B6 value will result in speed control
- speed_n=(log10(speed)/log10(2)); //thus speed_n contains which(nth) multiple of 2 is the data read at Port B
- for(i=1;i<=128;i*=2)
- {
- if(polarity)
- PORTD=~i; //if B7 is high, then polarity=1, and Port D displays inverse of i
- else
- PORTD=i;
- _delay_ms(100+(speed_n*50));
- }
- for(i=128;i>1;i-=i/2)
- {
- if(polarity)
- PORTD=~i;
- else
- PORTD=i;
- _delay_ms(100+(speed_n*50));
- }
- }return(1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement