Don't ad-block us - support your favorite websites. We have safe, unobstrusive, robotics related ads that you actually want to see - see here for more.
0 Members and 1 Guest are viewing this topic.
/* * pwm.c * * Created: 24/09/2011 11:02:02 * Author: Maz */ #define F_CPU 1000000UL#include <avr/io.h>#include <avr/interrupt.h>#include <util/delay.h>int main(void){ PORTD |= 1 << PORTD2; // Set PD2 (INT0) high (pull - up) TCCR1A |= 1 << COM1A1; // Set OC1A on Compare Match when up counting, clear - when down counting TCCR1A |= 1 << WGM11; // PWM, phase and frequency correct TCCR1B |= 1 << WGM13; // PWM, phase and frequency correct TCCR1B |= 1 << CS10; // No prescaling ICR1 = 3; // PWM top value OCR1A = 0; // PWM compare value; EICRA |= 1 << ISC01; // The falling edge of INT0 (PD2) generates an interrupt request EIMSK |= 1 << INT0; // Enable INT0 external interrupt sei(); // Enable global interrupt DDRB |= 1 << DDB1; // Set PB1 (OSC1A) as output for(;;) {}}ISR(INT0_vect) // Routine when interrupt occurs{ while(PIND & (1 << PIND2)){} // Button pressed detection if(OCR1A < 3) { OCR1A++; // Increase PWM compare value by 1 (increase duty cycle) } else { OCR1A = 0; // Set PWM compare value to 0 (set duty cycle to 0%) } _delay_ms(250);}
/* * ADC.c * * Created: 01/10/2011 17:14:43 * Author: Zydrune ir Mazvydas */ #include <avr/io.h>#include <avr/interrupt.h>#include <util/delay.h>int main(void){ initPwm(); // Initialize PWM initAdc(); // Initialize ADC sei(); // Enable Global Interrupts for(;;){}}ISR(ADC_vect) // Routine when interrupt occurs{ OCR0A = ADCH;}int initAdc(void){ ADMUX |= 1 << REFS0; // Set ADC reference to AVCC ADMUX |= 1 << ADLAR; // Left adjust ADC result to allow easy 8bit reading ADCSRA |= 1 << ADPS2; // Set ADC prescaler to 16 - 62.5kHz sample rate @ 1MHz ADCSRA |= 1 << ADATE; // Set ADC to Free-Running Mode ADCSRA |= 1 << ADEN; // Enable ADC ADCSRA |= 1 << ADIE; // Enable ADC Interrupt ADCSRA |= 1 << ADSC; // Start A2D Conversions}int initPwm(void){ TCCR0A |= 1 << COM0A1; // Set OC0A on Compare Match when down counting, clear - when up counting TCCR0A |= 1 << WGM00; // PWM, phase correct TCCR0B |= 1 << CS00; // No prescaling OCR0A = 0; // PWM compare value; DDRD |= 1 << DDD6; // Set PB1 (OSC1A) as output}
/* * LCD.c * * Created: 09/10/2011 17:33:37 * Author: Zydrune ir Mazvydas */ #include <avr/io.h>int main(void){ setPins(); initLcd(); clearLCD(); write(); while(1) { }}int initLcd(void){ PORTC |= 1 << PORTC3; PORTD |= (1 << PORTD2) | (1 << PORTD3); enable();}int setPins(void){ DDRC |= (1 << DDC3) | (1 << DDC4) | (1 << DDC5); DDRD |= (1 << DDD0) | (1 << DDD1) | (1 << DDD2) | (1 << DDD3) | (1 << DDD4) | (1 << DDD5) | (1 << DDD6) | (1 << DDD7);}int clearLCD(void){ PORTD = 0b00000001; enable();}int enable(void){ PORTC &= ~(1 << PORTC3); for(int counter = 0; counter < 90; counter++){} PORTC |= 1 << PORTC3;}int rsOn(void){ PORTC |= 1 << PORTC5;}int rsOff(void){ PORTC &= ~(1 << PORTC5);}int rwOn(void){ PORTC |= 1 << PORTC4;}int rwOff(void){ PORTC &= ~(1 << PORTC4);}int write(void){ rsOn(); PORTD = 0b01001101;//M enable(); PORTD = 0b01111001;//y enable(); PORTD = 0b01101100;//l enable(); PORTD = 0b01101001;//i enable(); PORTD = 0b01110101;//u enable(); PORTD = 0b11111110;// "space" enable(); PORTD = 0b01011010;//Z enable(); PORTD = 0b01111001;//y enable(); PORTD = 0b01100100;//d enable(); PORTD = 0b01110010;//r enable(); PORTD = 0b01110101;//u enable(); PORTD = 0b01110100;//t enable(); PORTD = 0b01100101;//e enable();}