Thread: Keil programming,Heartrate program

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    7

    Keil programming,Heartrate program

    Heys guys,
    Attempting to do a heartbeat code,I've seen a whole load of various heartbeat programs on various websites so I'm trying to work with one thats seems the easiest to read,I've edited it around to suit my needs.
    Its pretty much just take in value from sensor,do calculation,output result to lcd.
    Code:
    #include reg51.h
    //#include
    void _nop_(void);
    #define delay_us _nop_();//generates 1 microsecond
    #define LCD P3
    sbit RS=P2^0;//connect p0.0 to rs pin of lcd
    sbit EN=P2^2;//connect p0.1 to en pin of lcd
    sbit RW=P2^1;
    sbit PULSE=P0^0;//pulse input from heart beat sensor
    //sbit LED=P1^0; //led indicator for pulse indication
    void integer_lcd(int);
    void init_lcd(void);
    void cmd_lcd(unsignedchar);
    void write_lcd(unsignedchar);
    void delay_ms(unsignedint);
    unsignedint num=0;
    unsignedint num1=0;
    unsignedint dig_1,dig_2,dig_3,dig_4,test=0;
    unsignedint dig_11,dig_21,dig_31,dig_41,test1=0,test2=0;
    unsignedchar k=0;
    void main(void)
    {
    init_lcd();
    //LED=0;
    write_lcd('H');
    write_lcd('E');
    write_lcd('A');
    write_lcd('R');
    write_lcd('T');
    write_lcd(' ');
    write_lcd('B');
    write_lcd('E');
    write_lcd('A');
    write_lcd('T');
    write_lcd(' ');
    write_lcd(' ');
    write_lcd(' ');
    write_lcd(' ');
    write_lcd(' ');
    write_lcd(' ');
    cmd_lcd(0xc0);
    write_lcd(' ');
    write_lcd(' ');
    write_lcd(' ');
    write_lcd(' ');
    write_lcd(' ');
    write_lcd(' ');
    write_lcd(' ');
    write_lcd(' ');
    write_lcd('C');
    write_lcd('O');
    write_lcd('U');
    write_lcd('N');
    write_lcd('T');
    write_lcd('E');
    write_lcd('R');
    delay_ms(300);
    cmd_lcd(0x01);
    write_lcd('B');
    write_lcd('e');
    write_lcd('a');
    write_lcd('t');
    write_lcd('s');
    write_lcd(':');
    write_lcd(48);
    write_lcd(48);
    write_lcd(48);
    write_lcd(48);
    while(1)
    {
    if(PULSE==1)//check for input pulse from sensor
    {
    cmd_lcd(0x01);
    test++;
    num=test;
    dig_4=num%10;
    num=num/10;
    dig_3=num%10;
    num=num/10;
    dig_2=num%10;
    dig_1=num/10;
    if(test==9999)
    test=0;
    write_lcd('B');
    write_lcd('e');
    write_lcd('a');
    write_lcd('t');
    write_lcd('s');
    write_lcd(':');
    write_lcd(dig_1+48);
    write_lcd(dig_2+48);
    write_lcd(dig_3+48);
    write_lcd(dig_4+48);
    //LED=1;
    delay_ms(50);
    //LED=0;
    }
    }
    }
    ////////////////////////////////////////////////////////////////
    void init_lcd(void)
    {
    delay_ms(10);//delay 10 milliseconds
    cmd_lcd(0x38);//8 bit initialize, 5x7 character font, 16x2 display
    cmd_lcd(0x0e);//lcd on, cursor on
    cmd_lcd(0x06);//right shift cursor automatically after each character is displayed
    cmd_lcd(0x01);//clear lcd
    }
    //transmit command or instruction to lcd
    void cmd_lcd(unsignedchar c)
    {
    EN=1;
    RW=0;//set enable pin
    RS=0;//clear register select pin
    LCD=c;//load 8 bit data
    EN=0;//clear enable pin
    delay_ms(2);//delay 2 milliseconds
    }
    //transmit a character to be displayed on lcd
    void write_lcd(unsignedchar c)
    {
    EN=1;//set enable pin
    RW=0;
    RS=1;//set register select pin
    LCD=c;//load 8 bit data
    EN=0;//clear enable pin
    delay_ms(2);//delay 2 milliseconds
    }
    //generates delay in milli seconds
    void delay_ms(unsignedint i)
    {
    unsignedint j;
    while(i-->0)
    {
    for(j=0;j<500;j++)
    {
    ;
    }
    }
    }
    I think this only works as a pulse counter though so to calculate the beats per minute a time reference is needed I think.
    When I load it onto my chip,the welcome message comes up on the lcd and then everytime I put my finger on the lrd the no of beats increment by 1 on the lcd screen.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I think this only works as a pulse counter though so to calculate the beats per minute a time reference is needed I think.
    You are correct. You need to read the data sheet for your device for detailed information on implementing the timer. Then you have to figure out how the pulse counter and timer are related, how to calculate a result based on these, and how to send this value to the display.

    Be warned: These are not trivial concepts. If you're not very skilled at 'C' programming and reading/understanding data sheets, then it will be very difficult, if not impossible, to make this code do what you want. If you're a beginner, then copying and modifying code from the internet is not a viable solution for anything beyond the most trivial of embedded programs.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    7
    Thanks for the pointers,I'll try anyway

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    For what it's worth, some general advice for your pursuit:



    1. Get more comfortable with C coding in general; specifically with console programming on the computer. If you don't already have one, purchase a good book, go through it carefully, take notes, type out all examples, and do all exercises. You should have a solid foundation of the basics before you try embedded programming in C.

    The main reason it's easier to learn the language through the console is that the results can be seen quickly, and a lot of information can be printed to the screen for thorough analysis.

    I apologize if I'm wrong about your level of C knowledge, but I'm assuming you're not that experienced because of things like this:

    Code:
    write_lcd('H');
    write_lcd('E');
    write_lcd('A');
    write_lcd('R');
    write_lcd('T');
    write_lcd(' ');
    write_lcd('B');
    write_lcd('E');
    write_lcd('A');
    write_lcd('T');
    write_lcd(' ');
    write_lcd(' ');
    write_lcd(' ');
    write_lcd(' ');
    write_lcd(' ');
    write_lcd(' ');
    ... which could easily be written in only a couple of lines with a loop.



    2. After you have a good grasp of the basics, the next learning curve requires dealing with the device, the hardware, and the programming environment. I gave some advice for this portion to someone else here a while back (see here).



    Best of luck.
    Last edited by Matticus; 04-07-2014 at 02:21 PM.

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    7
    Thats Matticus,I'll go through all you've said above.
    You're right on the experience part too

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C programming , program does't work
    By AmirHossein H-m in forum C Programming
    Replies: 2
    Last Post: 04-19-2013, 12:38 PM
  2. New to C Programming, need help in a program
    By mvthakar in forum C Programming
    Replies: 10
    Last Post: 10-18-2012, 01:21 PM
  3. Replies: 43
    Last Post: 03-01-2010, 01:48 PM
  4. Replies: 4
    Last Post: 09-06-2009, 06:56 PM
  5. New to programming, messed up program
    By mstrymn in forum C++ Programming
    Replies: 4
    Last Post: 06-25-2005, 02:38 PM