[moved] C8051340F micro controller - how to calculate Freq/Bpm and display on lcd

Status
Not open for further replies.

Brien Cheow

Newbie level 6
Joined
Apr 29, 2015
Messages
11
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
168
Hi sorry to bump this thread after so long but i need help with a final year project. So basically i am using C8051340F micro controller and i use silicon Laboratories ide to do a little programming. Was wondering how to calculate the Freq/Bpm and display it on 128x64 pixels lcd.. Thanks for help in advance.
Coding:


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
    Graphic LCD 64 by 128 pixels, Monochrome
    SP5-GFX1
     Connection to P2 of 8051  : Silicon Lab C8051F340
    #1 GND
    #2 +5V
    #3 SI  serial data -- P2.7
    #4 SCL serial clock -- P2.6
    #5 A0P  0 = select control, 1 = data port --- P2.5
    #6 RESET low to reset LCD panel -- P2.4
    #7 CS1  chip select, 0 = enable data transfer -- P2.3
    #8 BL-  Back light - GND
    #9 BL+  Back light - +5V
 
    Page memory for display : B0 to B7, eight pages, one page 128 bytes
        each byte for 8 rows x 1 column, bit 0 at row 0, bit 1 at row 1 etc.    
 
    last edited : 15 Jan 2015.
    Version 1.0
*/
//#include <reg51.h>
#include <c8051f340.h>                 // SFR declarations
 
//-----------------------------------------------------------------------------
// Global CONSTANTS
//-----------------------------------------------------------------------------
 
#define SYSCLK      12000000           // SYSCLK frequency in Hz
 
sfr16 ADC0 = 0xbd;      // ADC0 result register
sfr16 TMR2RL   = 0xca;                 // Timer2 reload value
sfr16 TMR2     = 0xcc;                 // Timer2 counter
 
/*
sbit LCD_SI = P2^1;
sbit LCD_SCL = P2^2;
sbit LCD_A0P = P2^3;
sbit LCD_RESET = P2^4;
sbit LCD_CS1 = P2^5;
*/
 
 
sbit LCD_SI = P2^7;
sbit LCD_SCL = P2^6;
sbit LCD_A0P = P2^5;
sbit LCD_RESET = P2^4;
sbit LCD_CS1 = P2^3;
 
int i,j,k,m;
 
// 5 columns by 8 rows ASCII font, starting from SPACE, 0x20
code unsigned char font[] = {
   0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x5f,0x00,0x00,   //  !
   0x00,0x03,0x00,0x03,0x00,0x14,0x7f,0x14,0x7f,0x14,   // "#
   0x24,0x2a,0x7f,0x2a,0x12,0x23,0x13,0x08,0x64,0x62,   // $%
   0x36,0x49,0x55,0x22,0x50,0x00,0x05,0x03,0x00,0x00,   // &'
   0x00,0x1c,0x22,0x41,0x00,0x00,0x41,0x22,0x1c,0x00,   // ()
   0x14,0x08,0x3e,0x08,0x14,0x08,0x08,0x3e,0x08,0x08,   // *+
   0x00,0x50,0x30,0x00,0x00,0x08,0x08,0x08,0x08,0x08,   // ,-
   0x00,0x60,0x60,0x00,0x00,0x20,0x10,0x08,0x04,0x02,   // ./
   0x3e,0x51,0x49,0x45,0x3e,0x00,0x42,0x7f,0x40,0x00,   // 01
   0x42,0x61,0x51,0x49,0x46,0x21,0x41,0x45,0x4b,0x31,   // 23
   0x18,0x14,0x12,0x7f,0x10,0x27,0x45,0x45,0x45,0x39,   // 45
   0x3c,0x4a,0x49,0x49,0x30,0x01,0x71,0x09,0x05,0x03,   // 67
   0x36,0x49,0x49,0x49,0x36,0x06,0x49,0x49,0x29,0x1e,   // 89
   0x00,0x36,0x36,0x00,0x00,0x00,0x56,0x36,0x00,0x00,   // :;
   0x08,0x14,0x22,0x41,0x00,0x14,0x14,0x14,0x14,0x14,   // <=
   0x00,0x41,0x22,0x14,0x08,0x02,0x01,0x51,0x09,0x06,   // >?
   0x32,0x49,0x79,0x41,0x3e,0x7e,0x11,0x11,0x11,0x7e,   // @A
   0x7f,0x49,0x49,0x49,0x36,0x3e,0x41,0x41,0x41,0x22,   // BC
   0x7f,0x41,0x41,0x22,0x1c,0x7f,0x49,0x49,0x49,0x41,   // DE
   0x7f,0x09,0x09,0x09,0x01,0x3e,0x41,0x49,0x49,0x7a,   // FG
   0x7f,0x08,0x08,0x08,0x7f,0x00,0x41,0x7f,0x41,0x00,   // HI
   0x20,0x40,0x41,0x3f,0x01,0x7f,0x08,0x14,0x22,0x41,   // JK
   0x7f,0x40,0x40,0x40,0x40,0x7f,0x02,0x0c,0x02,0x7f,   // LM
   0x7f,0x04,0x08,0x10,0x7f,0x3e,0x41,0x41,0x41,0x3e,   // NO
   0x7f,0x09,0x09,0x09,0x06,0x3e,0x41,0x51,0x21,0x5e,   // PQ
   0x7f,0x09,0x19,0x29,0x46,0x46,0x49,0x49,0x49,0x31,   // RS
   0x01,0x01,0x7f,0x01,0x01,0x3f,0x40,0x40,0x40,0x3f,   // TU
   0x1f,0x20,0x40,0x20,0x1f,0x3f,0x40,0x38,0x40,0x3f,   // VW
   0x63,0x14,0x08,0x14,0x63,0x07,0x08,0x70,0x08,0x07,   // XY
   0x61,0x51,0x49,0x45,0x43,0x00,0x7f,0x41,0x41,0x00,   // Z[
   0x02,0x04,0x08,0x10,0x20,0x00,0x41,0x41,0x7f,0x00,   // \]
   0x04,0x02,0x01,0x02,0x04,0x40,0x40,0x40,0x40,0x40,   // ^_
   0x00,0x01,0x02,0x04,0x00,0x20,0x54,0x54,0x54,0x78,   // `a
   0x7f,0x48,0x44,0x44,0x38,0x38,0x44,0x44,0x44,0x20,   // bc
   0x38,0x44,0x44,0x48,0x7f,0x38,0x54,0x54,0x54,0x18,   // de
   0x08,0x7e,0x09,0x01,0x02,0x0c,0x52,0x52,0x52,0x3e,   // fg
   0x7f,0x08,0x04,0x04,0x78,0x00,0x44,0x7d,0x40,0x00,   // hi
   0x20,0x40,0x44,0x3d,0x00,0x7f,0x10,0x28,0x44,0x00,   // jk
   0x00,0x41,0x7f,0x40,0x00,0x7c,0x04,0x18,0x04,0x78,   // lm
   0x7c,0x08,0x04,0x04,0x78,0x38,0x44,0x44,0x44,0x38,   // no
   0x7c,0x14,0x14,0x14,0x08,0x08,0x14,0x14,0x18,0x7c,   // pq
   0x7c,0x08,0x04,0x04,0x08,0x48,0x54,0x54,0x54,0x20,   // rs
   0x04,0x3f,0x44,0x40,0x20,0x3c,0x40,0x40,0x20,0x7c,   // tu
   0x1c,0x20,0x40,0x20,0x1c,0x3c,0x40,0x30,0x40,0x3c,   // vw
   0x44,0x28,0x10,0x28,0x44,0x0c,0x50,0x50,0x50,0x3c,   // xy
   0x44,0x64,0x54,0x4c,0x44,0x00,0x08,0x36,0x41,0x00,   // z{
   0x00,0x00,0x7f,0x00,0x00,0x00,0x41,0x36,0x08,0x00,   // |}
   0x10,0x08,0x08,0x10,0x08,0x00,0x00,0x02,0x05,0x02 }; // ^degree
 
 
//-----------------------------------------------------------------------------
// Function PROTOTYPES
//-----------------------------------------------------------------------------
 
void delay(int d);             // delay by d ms
void delaya(void);             // a short delay
void SYSCLK_Init (void);       // set CPU clock
void PORT_Init (void);         // init CPU ports
void LCD_reset(void);          // reset LCD
void LCD_command(unsigned char);        // Send command code to LCD  
void LCD_data(unsigned char);       // ASCII character to be displayed on LCD 
void SPI_send(unsigned char);       // shift 8 bit data to LCD SPI interface, high bit first
void write_char(unsigned char);     // display an ASCII code at current position
void write_msg_xy(unsigned char, unsigned char, unsigned char*); // write strting at x col, y row
void set_xy(unsigned char x, unsigned char y);  // set cursor position
void plot_xy(unsigned char x, unsigned char y);
 
//-----------------------------------------------------------------------------
 
 
void timer0_interrupt(void) interrupt 1 using 1   //auto reload every 50us
{
 
}
 
 
void LCD_Clear_Display()
{
    for(k=0; k<8; k++)     // clear display
    {
        LCD_command(0xB0 + k); delay(20);
        LCD_command(0x10);  //high nibble of column address
        LCD_command(0x00);  // low 4 bits of column address
      for(i=0; i<128; i++)
      {
        LCD_data(0x00);  
      }
    }
}
 
void delay(int d)  // delay for d ms
{
   int i, j;
   for(i=0; i<d; i++)
     for(j=0; j<1179; j++);
}
 
void delaya()     //a short delay
{
  char j;
  for(j=0; j<1; j++);
}
 
void main (void) 
{
   PCA0MD &= ~0x40;                    // WDTE = 0 (clear watchdog timer 
                                       // enable)
   PORT_Init();                        // Initialize Port I/O
   SYSCLK_Init ();                     // Initialize Oscillator
 
// ADC init.
   ADC0CN = 0x00;                      // ADC0 disabled, normal tracking, 
                                       // conversion triggered on TMR2 overflow
   REF0CN = 0x03;                      //Internal voltage reference for ADC 
   AMX0P = 0x00;                       //   0x05=P2^6.. 0x13 ADC0 positive input = P1.1
   AMX0N = 0x1F;                       // ADC0 negative input = GND
                                       // i.e., single ended mode
   ADC0CF = ((SYSCLK/3000000)-1)<<3;   // set SAR clock to 3MHz
   ADC0CF |= 0x00;                     // right-justify results 
   AD0EN = 1; 
   
  // SFRPAGE = TIMER01_PAGE;             // Set SFR page
 
   TH0 = -151;  // -149;                        // Init Timer0 High register
   TL0 = TH0;                          // Set the intial Timer0 value
   ET0=1;                              // Timer0 interrupt enabled
   TCON = 0x10;                        // Timer0 ON
   TMOD = 0x02;                        // Timer0 in 8-bit reload mode
    EA = 1;       // allows interrupt
 
   delay(200);
 
 
 // initialize graphic LCD, set 8x8 fonts and graphic, OR mode for text and graphic
    LCD_reset();  
    LCD_command(0xA2);     // LCD bias, 1/9 bias
    LCD_command(0xA0);     // set normal display mode
    LCD_command(0xC8);     // set output mode
    LCD_command(0x23);     // set voltage regulator internal resistor
    LCD_command(0x81);     // electronic voltage double - two byte instruction
    LCD_command(0x1D);     // 6 bits, 0 to 0x2F, increase contrast
    LCD_command(0x2B);     // set power control circuit
    LCD_command(0x40);     // set start line = 0
    LCD_command(0xAF);     // setdisplay on
    LCD_command(0x10);     // lower 4 bits for high nibble of column address, 0 to 127
    LCD_command(0x00);     // lower 4 bits for low nibble of column address
    LCD_command(0xB0);     // set page address, B0 to B7 for 8 pages
    LCD_Clear_Display();    //clear LCD screen
 
 
 
     write_msg_xy(0, 0, "BVP(C)2015 NYP/BEMG 70BPM");
     delay(200);
 
     
// enable timer interrupts
//  ET0=1;
//  TMOD=0x21;
//  TR0=1;
//  EA=1;
 
  while(1)
  {
    for(k=0; k<127; k++)
    {
 
      m= 63 - (ADC0>>4);    //take high 6 bits from the 10-bit adc
      plot_xy(k, m);
      plot_xy(k, m-1);  // add two more dots for thicker lines
      plot_xy(k, m+1);
      delay(30);
      AD0BUSY = 1;  // start ADC conversion
/*
      if ((k & 0x1F) == 0 )          //draw straight lines
      {
        for(j=0; j<4; j++)
        {
            set_xy(k,4+j);  
            LCD_data(0xFF);
         }
      }
*/
    }
 
    
    LCD_Clear_Display();
    write_msg_xy(100, 55, "72BPM");
 
  } // end while(1) 
} // end main 
 
 
// configure I/O ports
void PORT_Init (void)
{
/*
    P1SKIP=0x20;
    P1MDOUT = 0x0F;         
    P2MDOUT = 0x3F;
  //  P3MDOUT = 0xFF;           // for ULN2803 drivers
  //  P4MDOUT = 0xFF;
  //  P0MDOUT = 0xFF;           // for LTC1298, LCD interfaces
    P2MDIN &= 0x0BF;        // P2.6 as ADC0
 
    P0MDOUT |= 0x10;        // Enable UTX as push-pull output
    XBR0     = 0x01;        // Enable UART on P0.4(TX) and P0.5(RX)                     
    XBR1     = 0x40;        //0x40; Enable crossbar and weak pull-ups
//  P2 = 0xFF;
//  P3 = 0xFF;
//  P4 = 0xFF;
*/
 
    P1MDOUT = 0x00;         // enable P1 outputs
    P2MDOUT = 0xFE;
    P3MDOUT = 0xFF;         // for ULN2803 drivers
    P4MDOUT = 0xFF;
    P0MDOUT = 0xFF;         // for LTC1298, LCD interfaces
 
//  P0MDOUT |= 0x10;        // Enable UTX as push-pull output
//      XBR0     = 0x01;        // Enable UART on P0.4(TX) and P0.5(RX)                     
    XBR1     = 0x40;        //0x40; Enable crossbar and weak pull-ups
    P2 = 0xFF;
    P3 = 0xFF;
    P4 = 0xFF;
}
 
//-----------------------------------------------------------------------------
// initializes the system clock to use the internal oscillator
void SYSCLK_Init (void)
{
   OSCICN |= 0x03;                     // Configure internal oscillator 
   RSTSRC  = 0x04;                     // Enable missing clock detector
}
 
 
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// LCD routnies 
// send one byte to SPI interface
void SPI_send(unsigned char dd)
{
unsigned char i;
    LCD_CS1=0;
    for(i=0; i<8; i++)
    {
       LCD_SCL=0;       // set clock pulse low first
       if( dd  & 0x80 )  // set data bit
        LCD_SI=1;
       else
        LCD_SI=0;
       LCD_SCL=1;       // clock high
       dd = dd <<= 1;   // shifted to next bit
    }
    LCD_CS1=1;
    delaya();
}
 
 
void LCD_reset()
{
  LCD_RESET = 0;
  delay(10);
  LCD_RESET = 1;
  delay(10);
}
 
 
void LCD_command(unsigned char ch) {    //send a command byte to LCD 
    LCD_A0P = 0;
    SPI_send(ch);
    delaya();
}
 
void LCD_data(unsigned char ch) {    //send a data byte to LCD data port
    LCD_A0P = 1;
    SPI_send(ch);
}
 
// print an ASCII code on LCD at current text position
void write_char(unsigned char ch)
{
unsigned char * fontptr, k;
    fontptr = font + (ch-0x20) * 5;       // get the font pattern, 8 by 5
    for(k=0; k<5; k++)
      LCD_data( *fontptr++);
}
 
 
// display a message at column x, row y
void write_msg_xy(unsigned char x, unsigned char y, unsigned char *chptr)
{
char * ptr;
    ptr = chptr;
    set_xy(x, y);
    while(*ptr)
      write_char(*ptr++);
}
 
void plot_xy(unsigned char x, unsigned char y)  // draw a dot at position x, y, x: 0 to 127, y:0 to 63
{
char ypage, dd=1;
    ypage = y >>3;
    set_xy(x, ypage);
    ypage = y & 0x7; //low 3 bits for dot position
    dd = dd << ypage;
      LCD_data(dd);
}
 
 
// set LCD display position
void set_xy(unsigned char x, unsigned char y)
{
    LCD_command(0x10 | ( x>> 4) & 0x07  );  // lower 4 bits for high nibble of column address, 0 to 127
    LCD_command(0x00 | x & 0x0F);   // lower 4 bits for low nibble of column address
    LCD_command(0xB0 | y & 0x07);  // set page address, B0 to B7 for 8 pages
}
 
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 
Last edited by a moderator:

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…