Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- *
- * assing1.c
- * Author: Kevin McCarthy & Geoff Bolton
- *
- */
- #include <avr/io.h>
- #include <avr/interrupt.h>
- unsigned int timecount0;
- unsigned int delay_time; // LED Timer delay
- unsigned char up = 1; // Determines if LEDS are going up or down
- #define speed1 410 // Speed1 = 2 volts into AIN0
- #define speed2 717 // Speed2 = 3.5 volts into AIN0
- unsigned int adc_reading; // AIN0 Comparison
- int main(void)
- {
- //Set PortD to all outputs because LEDs are connected to this PORT
- DDRD = 0xff; // 0xff = 0b11111111; all ones
- PORTD = 0x01; // Initialise to all off
- // Initialisation code for timer
- timecount0 = 0; // Initialise the overflow count. Note its scope
- TCCR0B = (5<<CS00); // Set T0 Source = Clock (16MHz)/1024 and put Timer in Normal mode
- TCCR0A = 0; // Not strictly necessary as these are the reset states but it's good
- // practice to show what you're doing
- TCNT0 = 61; // Recall: 256-61 = 195 & 195*64us = 12.48ms, approx 12.5ms
- TIMSK0 = (1<<TOIE0); // Enable Timer 0 interrupt
- // Begin ADC initialisation code
- ADMUX = ((1<<REFS0) | (0<<MUX0)); /* AVCC selected for VREF, ADC0 as ADC input */
- ADCSRA = ((1<<ADEN)|(1<<ADSC)|(1<<ADATE)|(1<<ADIE)|(7<<ADPS0));
- /* Enable ADC, Start Conversion, Auto Trigger enabled,
- Interrupt enabled, Prescale = 128 */
- ADCSRB = (0<<ADTS0); /* Select AutoTrigger Source to Free Running Mode
- Strictly speaking - this is already 0, so we could omit
- the write to ADCSRB, but included here so the intent is clear */
- sei(); // Global interrupt enable (I=1)
- while(1) // Do nothing loop
- ;
- }
- ISR(TIMER0_OVF_vect)
- {
- TCNT0 = 61; // TCNT0 needs to be set to the start point each time
- ++timecount0; // count the number of times the interrupt has been reached
- if (timecount0 >= delay_time) // check if delayTime has been exceeded
- {
- if (up == 1) { // Check if LEDs are rising or falling
- PORTD = PORTD<<1; // Bit shift PORTD register left (move LED)
- if (PORTD >= 128){ // Simple check if end has been reached
- up = 0; // Set to begin falling next
- }
- }
- else { // If not rising, begin falling
- PORTD = PORTD>>1; // Bit shift PORTD register right (move LED)
- if (PORTD <= 1) { // Check if at start again
- up = 1; // Set to begin rising next
- }
- }
- timecount0 = 0; // Restart the overflow counter
- }
- }
- ISR (ADC_vect) //handles ADC interrupts
- {
- adc_reading = ADC; // ADC is in Free Running Mode -
- // you don't have to set up anything for the next conversion
- if (adc_reading > speed2) // Check if AIN0 is greater than 3.5 volts
- delay_time = 320; // Set delay_time to 4 seconds
- else if (adc_reading > speed1) // Check if AIN0 is greater than 2 volts
- delay_time = 160; // Set delay time to 2 seconds
- else // Otherwise AIN0 is less than 2 volts
- delay_time = 40; // set delay time to 0.5 seconds
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement