uwezi

20211114_pwm_demo

May 31st, 2022 (edited)
869
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.89 KB | None | 0 0
  1. /*
  2.  * Created: 2021-11-14 16:22:34
  3.  * Author : uwezi
  4.  */
  5.  
  6. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  7. 4-digit 7-segment LED display with common anodes
  8.  
  9. - digit cathodes connected directly to PB0, PB1, PB2, PB3
  10. - segments A-PD0, B-PD1, C-PD2, D-PD3, E-PD4, F-PD5 and G-PD6
  11.   connected with one 220 ohm resistor per segment between port pin and LED
  12.  
  13. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  14. #define F_CPU 1000000UL
  15.  
  16. #include <avr/io.h>
  17. #include <util/delay.h>
  18. #include <avr/interrupt.h>
  19.  
  20. // 7-segment font for numbers 0-9
  21. uint8_t font[10] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f};
  22.  
  23. // use to buffer contents of display
  24. volatile uint8_t framebuffer[4];    
  25.  
  26. // which digit is active now
  27. volatile uint8_t current_digit = 0;  
  28.  
  29. // display the contents of framebuffer on the LED display by multiplexing
  30. ISR(TIMER0_OVF_vect)
  31. {
  32.   // the code for the interrupt service routine
  33.   PORTD = 0xff;                  // all segments off
  34.   current_digit += 1;            // step one digit
  35.   current_digit %= 4;            //    and keep between 0-3
  36.   PORTB = (PORTB & 0b00001111) | (0b00010000 << current_digit);
  37.   PORTD = framebuffer[current_digit];  
  38. }
  39.  
  40. void init(void)
  41. {
  42.   // we can collect all hardware initialization here
  43.   DDRB = 0b11110000;    // lower B-pins as output
  44.   DDRD = 0b11111111;    // all D-pins as output
  45.  
  46.   framebuffer[0] = 0xff;
  47.   framebuffer[1] = 0xff;
  48.   framebuffer[2] = 0xff,
  49.   framebuffer[3] = 0xff;
  50.  
  51.   // setting up TIMER0 (8 bit)
  52.   // - no pwm
  53.   // - prescaler 2:1, 500 kHz clock rate
  54.   // - normal counting mode 0...255
  55.   // - timer overflow interrupt enabled
  56.   TCCR0A = (0 << COM0A1) | (0 << COM0A0)  
  57.          | (0 << COM0B1) | (0 << COM0B0)  
  58.          | (0 << WGM01)  | (0 << WGM00);  
  59.  
  60.   TCCR0B = (0 << WGM02)                  
  61.          | (0 << CS02) | (1 << CS01) | (0 << CS00);  // clock prescaler
  62.  
  63.   // enable the TIMER0 OVERFLOW INTERRUPT
  64.   TIMSK0 = (0 << OCIE0B) | (0 << OCIE0A) | (1 << TOIE0);
  65.  
  66.   // setting up the ADC  
  67.   // - left aligned for 8-bit results
  68.   // - channel ADC0
  69.   ADMUX = (0 << REFS1)  | (1 << REFS0)
  70.         | (1 << ADLAR)
  71.         | (0 << MUX3)   | (0 << MUX2)  | (0 << MUX1) | (0 << MUX0);
  72.   ADCSRA = (1 << ADEN)  | (1 << ADSC)
  73.          | (0 << ADATE) | (0 << ADIF)  | (0 << ADIE)
  74.          | (0 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);      
  75.  
  76.   // setting up TIMER1
  77.   // - OC1A and OC1B running in non-inverting pwm
  78.   // - clock prescale 1:1, 1us per tick
  79.   // - WGM13..10 determine mode
  80.   //    0 0 0 1 phase correct pwm, 8-bit
  81.   //    0 1 0 1 fast pwm, 8-bit
  82.   TCCR1A = (1 << COM1A1) | (0 << COM1A0)
  83.          | (1 << COM1B1) | (0 << COM1B0)
  84.          | (0 << WGM11)  | (1 << WGM10);
  85.   TCCR1B = (0 << WGM13)  | (0 << WGM12)
  86.          | (0 << CS12)   | (0 << CS11)   | (1 << CS10);    
  87.              
  88.   // don't forget to enable interrupts
  89.   sei();
  90. }
  91.  
  92. // take the contents of value and put its numeric contents
  93. // into the 4 digits of the framebuffer for the display
  94. void update(uint16_t value)
  95. {
  96.   uint16_t A = value;
  97.   uint8_t  i;
  98.   for (i = 0; i < 4; i++)
  99.   {
  100.     // take the remainder of the division by 10
  101.     // look up the font for this number
  102.     // put it into the framebuffer
  103.     framebuffer[3-i] = ~font[A % 10];
  104.     A = A / 10;
  105.   }
  106. }
  107.  
  108. int main(void)
  109. {
  110.   uint16_t value = 9999;
  111.  
  112.   // do the hardware initialization
  113.   init();
  114.  
  115.   // set a constant width on OC1B of 40/256 or 79/511
  116.   OCR1B = 40;
  117.  
  118.   while (1)
  119.   {
  120.     // start an ADC conversion
  121.     ADCSRA = ADCSRA | (1 << ADSC);
  122.     while (ADCSRA & (1 << ADSC))
  123.     {
  124.       // we are still waiting
  125.     }
  126.     // just use the 8 highest bits 0...255
  127.     value = ADCH;
  128.    
  129.     // put the value into the framebuffer
  130.     update(value);
  131.    
  132.     // and set the pulse width of OC1A to the same value
  133.     OCR1A = value;
  134.    
  135.     // wait 10 ms between updates
  136.     _delay_ms(10);
  137.   }
  138. }
Add Comment
Please, Sign In to add comment