classicsat

Minimal 4 digit counter

Apr 10th, 2021 (edited)
422
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. // Direct port 4 bit multiplex to test 4 bit BCD to 7 segment decoder(or nixie decoder).
  2. // using port b, Arduino pin 8 as LSB, pin 11 as MSB, pin 12 as Decimal Point
  3. // port d, Arduino pins 2 to 4, as digit commons.
  4. //essence of Blink Without Delay for counter timing.
  5. // a little bit of work one could directly operate the segments (I use 74LS247 decoder)
  6.  
  7. int counter=0;// main counter
  8. byte digit=0; // digit counter
  9. byte digit_array[4];
  10.  
  11. unsigned long previousMillis = 0;
  12. const long interval = 500;
  13.  
  14.  
  15. void setup() {
  16. Serial.begin(9600);
  17. DDRB=0B00011111;// set lower 5 bits as output for decoder bits
  18. DDRD=0B00111100;// for anodes/digits
  19. inttoarray(counter);// pre populate digit array
  20. }
  21.  
  22. void loop() {
  23. // actually output to display here, one digit each pass
  24. PORTB=digit_array[digit]&15;// output to port, with mask of lower 4 bits
  25. PORTD=4<<digit;// because pin 2 is LSD, start at 4 (0B00000100), move left per digit.
  26.  
  27. // digit counter
  28. digit++;
  29. digit=digit&3;
  30. delay (1);
  31.  
  32.  
  33. unsigned long currentMillis = millis();
  34.  
  35. if (currentMillis - previousMillis >= interval) {
  36. previousMillis = currentMillis;
  37.  
  38. //counter stuff. Rolling over at 1023
  39. counter++;
  40. counter=counter&1023;
  41. // load counter into display array
  42. inttoarray(counter);
  43. }
  44.  
  45. }// end main loop
  46.  
  47. void inttoarray(int outnumber){
  48. for (byte pointer=0;pointer<4;pointer++){
  49. digit_array[pointer]=(outnumber%10);
  50. outnumber=outnumber/10;
  51. }
  52. }
Add Comment
Please, Sign In to add comment