uuu000

oled Frequency Counter

Apr 19th, 2023
523
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.13 KB | None | 0 0
  1. /*
  2.  * oled Frequency Counter
  3.  *
  4.  * Created: 10/01/2021
  5.  *  Author: moty22.co.uk
  6.  */
  7. #include <Wire.h>
  8. #include "font_8x5.c"
  9. #define yi 1  //Y of count display
  10.  
  11. unsigned char addr=0x3C;  //0x78
  12.  
  13. void setup() {
  14.  
  15.           //set timer0 in=250Hz out=1Hz
  16.     OCR0A = 249;    //divide by 250
  17.     TCCR0A=0b1000011;  //
  18.     TCCR0B=0b1110;  //  PWM mode, input T0 pin D4
  19.     pinMode(6, OUTPUT);// PD6  12
  20.    
  21.       // set timer2 in=16MHz out=250Hz
  22.     OCR2A =249;
  23.     OCR2B = 125;    //50% duty cycle
  24.     TCCR2A=0b110011;  //output B in phase, fast PWM mode
  25.     TCCR2B=0b1110; // set prescaler to 256 and start the timer
  26.     pinMode(3, OUTPUT); // PD3   5
  27.    
  28.         //  set timer1
  29.     OCR1A = 32767;   //32768 counts
  30.     TCCR1A = 0b1000011;  
  31.     TCCR1B = 0b11110; //input pin D5
  32.     pinMode(9, OUTPUT);// PB1  15
  33.  
  34.     Wire.begin();        // init i2c bus
  35.     Wire.setClock(100000);  //200khz
  36.     oled_init();
  37.     clrScreen();
  38. }
  39.  
  40. void loop()
  41. {
  42.   unsigned char d[7], nz, i;
  43.   byte overF=0;
  44.   unsigned long total, period;
  45.  
  46.     nz=1;
  47.       //wait for high output
  48.     while(digitalRead(6)){}
  49.     while(!digitalRead(6)){}
  50.       //start the count
  51.     TIFR1 = _BV(OCF1A);    //reset int
  52.     OCR1A = 32767;
  53.     TCNT1=0;
  54.     overF=0;
  55.     while(digitalRead(6)){
  56.     if(TIFR1 & _BV(1)) {++overF; TIFR1 |= _BV(1);}   //on overflow reset interrupt
  57.     }
  58.         //count end
  59.     total = (unsigned long)TCNT1 + ((unsigned long)overF * 32768);  //sum up registers
  60.  
  61.         //convert to 7 decimal digits
  62.     d[6]=total/1000000 + 16;    //1MHz digit, 16 is the ofset of 0 in font array
  63.     d[5]=(total/100000) %10 + 16;  //100KHz digit
  64.     d[4]=(total/10000) %10 + 16;
  65.     d[3]=(total/1000) %10 + 16;
  66.     d[2]=(total/100) %10 + 16;
  67.     d[1]=(total/10) %10 + 16;    //10Hz digit  
  68.     d[0]=total %10 + 16;
  69.  
  70.     if(total > 999999){drawChar2(d[6], yi, 0); drawChar2(12, yi, 10);}else{drawChar2(0, yi, 0); drawChar2(0, yi, 10);}    //12=, 0=blank
  71.     if(total > 99999){drawChar2(d[5], yi, 18);}else{drawChar2(0, yi, 18);}
  72.     if(total > 9999){drawChar2(d[4], yi, 30);}else{drawChar2(0, yi, 30);}
  73.     if(total > 999){drawChar2(d[3], yi, 42);  drawChar2(12, yi, 52);}else{drawChar2(0, yi, 42); drawChar2(0, yi, 52);}
  74.     drawChar2(d[2], yi, 60);
  75.     drawChar2(d[1], yi, 72);
  76.     drawChar2(d[0], yi, 84);
  77.     drawChar2(40, 1, 104); //H
  78.     drawChar2(90, 1, 116); //z
  79.  
  80.     nz=1;
  81.     if(total<1000){
  82.         period=1000000/total;
  83.         drawChar2(85, 5, 104); //u
  84.     }else{
  85.         period=1000000000/total;
  86.         drawChar2(78, 5, 104);}    //n
  87.     if(total==0)period=0;    
  88.  
  89.     //convert to 7 decimal digits
  90.     d[6]=period/1000000 + 16;   //1MHz digit
  91.     d[5]=(period/100000) %10 + 16;  //100KHz digit
  92.     d[4]=(period/10000) %10 + 16;
  93.     d[3]=(period/1000) %10 + 16;
  94.     d[2]=(period/100) %10 + 16;
  95.     d[1]=(period/10) %10 + 16;    //10Hz digit  
  96.     d[0]=period %10 + 16;
  97.  
  98.     for(i=7;i>1;i--){
  99.         if(d[i-1]==16 && nz){drawChar2(0, 5, (7-i)*12+12);} //if leading 0 display blank
  100.         if(d[i-1]>16 && nz){nz=0; }
  101.         if(!nz){drawChar2(d[i-1], 5, (7-i)*12+12);}
  102.     }
  103.      drawChar2(d[0], 5, 84);  //display digit 0
  104.  
  105.      drawChar2(51, 5, 116); //S
  106.      
  107. }
  108.  
  109. void clrScreen()    //fill screen with 0
  110. {
  111.     unsigned char y, x;
  112.  
  113.     for ( y = 0; y < 8; y++ ) {
  114.       for (x = 0; x < 17; x++){
  115.           command(0x21);     //col addr
  116.           command(8 * x); //col start
  117.           command(8 * x + 7);  //col end
  118.           command(0x22);    //0x22
  119.           command(y); // Page start
  120.           command(y); // Page end
  121.          
  122.           Wire.beginTransmission(addr);
  123.           Wire.write(0x40);
  124.           for (unsigned char i = 0; i < 8; i++){
  125.                Wire.write(0x00);          
  126.           }
  127.           Wire.endTransmission();        
  128.       }
  129.      
  130.     }
  131. }
  132.  
  133. void command( unsigned char comm){
  134.     Wire.beginTransmission(addr);
  135.     Wire.write(0x00);    
  136.     Wire.write(comm); // LSB
  137.     Wire.endTransmission();      
  138. }
  139.  
  140. void oled_init() {
  141.    
  142.     command(0xAE);   // DISPLAYOFF
  143.     command(0x8D);         // CHARGEPUMP *
  144.     command(0x14);     //0x14-pump on
  145.     command(0x20);         // MEMORYMODE
  146.     command(0x0);      //0x0=horizontal, 0x01=vertical, 0x02=page
  147.     command(0xA1);        //SEGREMAP * A0/A1=top/bottom
  148.     command(0xC8);     //COMSCANDEC * C0/C8=left/right
  149.     command(0xDA);         // SETCOMPINS *
  150.     command(0x12);   //0x22=4rows, 0x12=8rows
  151.     command(0x81);        // SETCONTRAST
  152.     command(0x9F);     //0x8F
  153.     //following settings are set by default
  154. //    command(0xD5);  //SETDISPLAYCLOCKDIV
  155. //    command(0x80);  
  156. //    command(0xA8);       // SETMULTIPLEX
  157. //    command(0x3F);     //0x1F
  158. //    command(0xD3);   // SETDISPLAYOFFSET
  159. //    command(0x0);  
  160. //    command(0x40); // SETSTARTLINE  
  161. //    command(0xD9);       // SETPRECHARGE
  162. //    command(0xF1);
  163. //    command(0xDB);      // SETVCOMDETECT
  164. //    command(0x40);
  165. //    command(0xA4);     // DISPLAYALLON_RESUME
  166. //    command(0xA6);      // NORMALDISPLAY
  167.     command(0xAF);          //DISPLAYON
  168.  
  169. }
  170.  
  171.     //size x2 chars
  172. void drawChar2(char fig, unsigned char y, unsigned char x)
  173. {
  174.     unsigned char i, line, btm, top;    //
  175.    
  176.     command(0x20);    // vert mode
  177.     command(0x01);
  178.  
  179.     command(0x21);     //col addr
  180.     command(x); //col start
  181.     command(x + 9);  //col end
  182.     command(0x22);    //0x22
  183.     command(y); // Page start
  184.     command(y+1); // Page end
  185.    
  186.     Wire.beginTransmission(addr);
  187.     Wire.write(0x40);
  188.  
  189.     for (i = 0; i < 5; i++){
  190.         line=font[5*(fig)+i];  //fig+16
  191.         btm=0; top=0;
  192.             // expend char    
  193.         if(line & 64) {btm +=192;}
  194.         if(line & 32) {btm +=48;}
  195.         if(line & 16) {btm +=12;}          
  196.         if(line & 8) {btm +=3;}
  197.        
  198.         if(line & 4) {top +=192;}
  199.         if(line & 2) {top +=48;}
  200.         if(line & 1) {top +=12;}
  201.                
  202.          Wire.write(top); //top page
  203.          Wire.write(btm);  //second page
  204.          Wire.write(top);
  205.          Wire.write(btm);
  206.  
  207.     }
  208.     Wire.endTransmission();
  209.        
  210.     command(0x20);      // horizontal mode
  211.     command(0x00);    
  212.        
  213. }
  214.  
Advertisement
Add Comment
Please, Sign In to add comment