Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. /*
  2. *
  3. * assing1.c
  4. * Author: Kevin McCarthy & Geoff Bolton
  5. *
  6. */
  7.  
  8. #include <avr/io.h>
  9. #include <avr/interrupt.h>
  10.  
  11. unsigned int timecount0;
  12. unsigned int delay_time; // LED Timer delay
  13. unsigned char up = 1; // Determines if LEDS are going up or down
  14.  
  15. #define speed1 410 // Speed1 = 2 volts into AIN0
  16. #define speed2 717 // Speed2 = 3.5 volts into AIN0
  17. unsigned int adc_reading; // AIN0 Comparison
  18.  
  19.  
  20. int main(void)
  21. {
  22. //Set PortD to all outputs because LEDs are connected to this PORT
  23.  
  24. DDRD = 0xff; // 0xff = 0b11111111; all ones
  25. PORTD = 0x01; // Initialise to all off
  26.  
  27. // Initialisation code for timer
  28.  
  29. timecount0 = 0; // Initialise the overflow count. Note its scope
  30. TCCR0B = (5<<CS00); // Set T0 Source = Clock (16MHz)/1024 and put Timer in Normal mode
  31.  
  32. TCCR0A = 0; // Not strictly necessary as these are the reset states but it's good
  33. // practice to show what you're doing
  34. TCNT0 = 61; // Recall: 256-61 = 195 & 195*64us = 12.48ms, approx 12.5ms
  35. TIMSK0 = (1<<TOIE0); // Enable Timer 0 interrupt
  36.  
  37. // Begin ADC initialisation code
  38.  
  39. ADMUX = ((1<<REFS0) | (0<<MUX0)); /* AVCC selected for VREF, ADC0 as ADC input */
  40. ADCSRA = ((1<<ADEN)|(1<<ADSC)|(1<<ADATE)|(1<<ADIE)|(7<<ADPS0));
  41. /* Enable ADC, Start Conversion, Auto Trigger enabled,
  42. Interrupt enabled, Prescale = 128 */
  43. ADCSRB = (0<<ADTS0); /* Select AutoTrigger Source to Free Running Mode
  44. Strictly speaking - this is already 0, so we could omit
  45. the write to ADCSRB, but included here so the intent is clear */
  46.  
  47. sei(); // Global interrupt enable (I=1)
  48.  
  49. while(1) // Do nothing loop
  50. ;
  51. }
  52.  
  53. ISR(TIMER0_OVF_vect)
  54. {
  55. TCNT0 = 61; // TCNT0 needs to be set to the start point each time
  56. ++timecount0; // count the number of times the interrupt has been reached
  57.  
  58. if (timecount0 >= delay_time) // check if delayTime has been exceeded
  59. {
  60. if (up == 1) { // Check if LEDs are rising or falling
  61. PORTD = PORTD<<1; // Bit shift PORTD register left (move LED)
  62. if (PORTD >= 128){ // Simple check if end has been reached
  63. up = 0; // Set to begin falling next
  64. }
  65. }
  66. else { // If not rising, begin falling
  67. PORTD = PORTD>>1; // Bit shift PORTD register right (move LED)
  68. if (PORTD <= 1) { // Check if at start again
  69. up = 1; // Set to begin rising next
  70. }
  71. }
  72.  
  73. timecount0 = 0; // Restart the overflow counter
  74. }
  75. }
  76.  
  77.  
  78. ISR (ADC_vect) //handles ADC interrupts
  79. {
  80. adc_reading = ADC; // ADC is in Free Running Mode -
  81. // you don't have to set up anything for the next conversion
  82.  
  83. if (adc_reading > speed2) // Check if AIN0 is greater than 3.5 volts
  84. delay_time = 320; // Set delay_time to 4 seconds
  85. else if (adc_reading > speed1) // Check if AIN0 is greater than 2 volts
  86. delay_time = 160; // Set delay time to 2 seconds
  87. else // Otherwise AIN0 is less than 2 volts
  88. delay_time = 40; // set delay time to 0.5 seconds
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement