Monika__

led counter 0-99 embedded systems

Nov 22nd, 2020
523
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdint.h>
  2. #include <stdbool.h>
  3. #include "include/AT91SAM9263.h"
  4.  
  5. #define PIO_PER_B AT91C_PIOB_PER
  6. #define PIO_OER_B AT91C_PIOB_OER
  7. #define PIO_CODR_B AT91C_PIOB_CODR
  8. #define PIO_SODR_B AT91C_PIOB_SODR
  9.  
  10. #define A (1<<25)  //pin PB25
  11. #define B (1<<24) // pin  PB24
  12. #define C (1<<22) // pin PB22
  13. #define D (1<<21) // pin PB21
  14. #define E (1<<20) // pin PB20
  15. #define F (1<<27) // pin  PB27
  16. #define G (1<<26) // pin PB26
  17.  
  18. #define LEFT_DISPLAY (1<<28) // controls Q2 transistor, left LED
  19. #define BRIGHTNESS (1<<29) // controls transistor Q1
  20. #define RIGHT_DISPLAY (1<<30) // controls Q3 transistor, right LED
  21.  
  22. #define LEFT 1
  23. #define RIGHT 0
  24.  
  25. #define DELAY 1000
  26.  
  27.  //uint32_t numeric type that guarantees 32 bits, value unsigned
  28. uint32_t ledDecode (uint8_t digit)
  29. {
  30.     int number[10] = {};
  31.     number[0] = A|B|C|D|E|F;
  32.     number[1] = B|C;
  33.     number[2] = A|B|D|E|G;
  34.     number[3] = A|B|C|G|G|D;
  35.     number[4] = F|B|G|C;
  36.     number[5] = A|F|G|C|D;
  37.     number[6] = A|F|G|E|D|C;
  38.     number[7] = A|B|C;
  39.     number[8] = A|B|C|D|E|F|G;
  40.     number[9] = A|B|F|G|C|D;
  41.    
  42.     return number[digit];
  43. }
  44.  
  45. void delay_ms (int delay) {
  46.         volatile int time;
  47.         for(time = 0; time < 25000*delay; time++);
  48. }
  49.  
  50. void LED_Init(void) {
  51.     *PIO_PER_B = (LEFT_DISPLAY|BRIGHTNESS|RIGHT_DISPLAY|A|B|C|D|E|F|G);
  52.     *PIO_OER_B = (LEFT_DISPLAY|BRIGHTNESS|RIGHT_DISPLAY|A|B|C|D|E|F|G);
  53.     *PIO_CODR_B  = (A|B|C|D|E|F|G);
  54.     *PIO_SODR_B = (LEFT_DISPLAY|RIGHT_DISPLAY);
  55.     *PIO_CODR_B = BRIGHTNESS;
  56. }
  57.  
  58. void LED_display_digit(uint8_t digit, uint8_t pos) {
  59.         *PIO_SODR_B = (LEFT_DISPLAY|RIGHT_DISPLAY); //turn off the displays
  60.         *PIO_CODR_B=(A|B|C|D|E|F|G); //clear segments
  61.         *PIO_SODR_B=(ledDecode(digit)); //display number
  62.         if(pos == LEFT) {
  63.             *PIO_CODR_B= LEFT_DISPLAY; //enable left display
  64.         } else {
  65.             *PIO_CODR_B= RIGHT_DISPLAY; //enable right display
  66.         }
  67. }
  68.  
  69. void firstPart(void) {
  70.     int counter = 0;
  71.     while(1)
  72.     {
  73.         LED_display_digit(counter, LEFT);
  74.         delay_ms(DELAY);
  75.         counter++;
  76.         if(counter == 10) {
  77.             counter = 0;
  78.         }
  79.     }
  80. }
  81.  
  82. void secondPart(void) {  
  83.     int counter = 0;
  84.     while(1) {
  85.         int tens = counter/10;
  86.         int unit = counter%10;
  87.         int i;
  88.         for(i=0; i<50; i++){
  89.             LED_display_digit(tens, LEFT);
  90.             delay_ms(10);
  91.             LED_display_digit(unit, RIGHT);
  92.             delay_ms(10);
  93.         }
  94.         counter++;
  95.         if(counter == 99) {
  96.             counter= 0;
  97.         }
  98.     }
  99. }
  100.  
  101. int main(void) {
  102.    
  103.     LED_Init();
  104.     //firstPart();
  105.     secondPart();
  106.  
  107.     return 0;
  108. }
  109.  
RAW Paste Data