Advertisement
hbinderup94

main og test AMS_Lab3

Feb 11th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.98 KB | None | 0 0
  1. /*
  2.  * LAB3_C.c
  3.  *
  4.  * Created: 31-01-2019 11:11:36
  5.  * Author : HBIND
  6.  */
  7.  #define F_CPU 16000000
  8. #include "lcd162.h"
  9. #include <avr/io.h>
  10. #include <util/delay.h>
  11. #include <avr/interrupt.h>
  12.  
  13. void Init_ADC();
  14. void Init_Timer1();
  15. void test_program();
  16.  
  17.  
  18. int main(void)
  19. {
  20.     /* Replace with your application code */
  21.     LCDInit();
  22.     LCDOnOffControl(0, 1);
  23.  
  24.     Init_ADC();
  25.     Init_Timer1();
  26.     sei();
  27.     test_program();
  28.  
  29.     while (1)
  30.     {
  31.        
  32.     }
  33. }
  34.  
  35. void test_program()
  36. {
  37.     LCDDispString("Let's test:");
  38.     _delay_ms(2000);
  39.     LCDGotoXY(0,1);
  40.     LCDDispString("DispChar: ");
  41.     LCDDispChar('G');
  42.     _delay_ms(1000);
  43.     LCDGotoXY(0,1);
  44.     LCDDispString("DispInt: ");
  45.     LCDDispInteger(46);
  46.     _delay_ms(1000);
  47.     LCDGotoXY(0,1);
  48.     LCDDispString("Blink/cursor:");
  49.     LCDOnOffControl(1, 0);
  50.     _delay_ms(1000);
  51.     LCDOnOffControl(0, 1);
  52.     _delay_ms(1000);
  53.     LCDGotoXY(0,1);
  54.     LCDDispString("Shift Screen:");
  55.     LCDShiftRight();
  56.     _delay_ms(1000);
  57.     LCDShiftLeft();
  58.     _delay_ms(1000);
  59.     LCDGotoXY(0,1);
  60.     LCDDispString("Move Cursor:  ");
  61.     LCDCursorRight();
  62.     _delay_ms(1000);
  63.     LCDCursorLeft();
  64.     _delay_ms(1000);
  65.     _delay_ms(5000);
  66.     LCDClear();
  67. }
  68.  
  69. void Init_ADC()
  70. {
  71.     // PF pins are inputs (ADC7-ADC0)
  72.     DDRF = 0;
  73.     // Internal 5 volt reference, ADLAR = 0, Input = ADC0 single ended (potentiometer)
  74.     ADMUX = 0b01000000;
  75.     // ADC enable
  76.     // ADC interrupt enabled
  77.     // Automatic start enabled
  78.     // ADC prescaler = 128 (=> ADC clock = 16 MHz / 128 = 125 kHZ)
  79.     ADCSRA = 0b10101111;
  80.     // Auto trigger source = Timer 1 overflow
  81.     ADCSRB = 0b00000110;
  82. }
  83.  
  84. void Init_Timer1()
  85. {
  86.     // 16000000 Hz /64 = 250 kHz
  87.     // Therefore we have 250000 "steps" per second
  88.     // - and want 0,25 seconds until next overflow
  89.     TCNT1 = 65536-62500;
  90.     // Timer 1 in Normal Mode and PS = 64
  91.     TCCR1A = 0b00000000;
  92.     TCCR1B = 0b00000011;
  93.     // Enable Timer 1 overflow interrupts
  94.     TIMSK1 |= 0b00000001;
  95. }
  96.  
  97. ISR(TIMER1_OVF_vect)
  98. {
  99.     //Reload Timer 1: 0,25 seconds to next overflow
  100.     TCNT1 = 65536-62500;
  101. }
  102.  
  103. ISR(ADC_vect)
  104. {
  105.     readKeys();
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement