This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

having problem with debugging a code (digital timer)

hi everyone
i'm using uvision4 and stm32f103ve
i have written and successfully compiled this code (0 errors)
but it does't work at simulation
it suppose when you press D4 (the key that increases the second)two times (it means that you intered 2 sec)after 2 sec it has to turn the led(which is connected to the C5)
i guess it has problem with delay function and its declaration.
Any help on this problem would be greatly appreciated!

#include "stm32f10x.h"
#include "system_stm32f10x.h"
#include "stm32f10x_conf.h"
#include "Delay.h"
#include "LED.h"
#include "Keys.h"
#include "lcd4bit.h"
#include <stdio.h>
#include <string.h>
                                                          //**************//
static __IO uint32_t TimingDelay;
uint8_t LCDBuff[50];
/* Private function prototypes -----------------------------------------------*/
void _delay_ms(__IO uint32_t nTime);
void _delay_us(__IO uint32_t nTime);
void TimingDelay_Decrement(void);
                              //**************//
int main(void){
         //****************************//
    SystemInit();       //72MHz
        //****************************//
        while(1){
        char A[16];
        int counter = 0;
        int i = 0;
        int m = 0 , h = 0;
        int flag = 1;
        void delay_init(u8 SYSCLK);
    lcd_init();         // vase lcd
        IOInit();      //baraye faal krdan clk va shenasandane pc5 , led.
        //delay_init(72000000);
        lcd_clear();
        KeysInit();   //faal krdane clk klid ha
        while(flag)     {
                 sprintf(A,"Time: %02u:%02u:%02u ",h,m,counter);
             lcd_clear();
             lcd_putsf(A);
                     if(PinA5 == 1){               //klide ejra
                                 flag = 0;
                                 break;
                                 }
                 if(PinA3 == 1) {                                 //entekhabe sanye daghighe va saat be
                                         delay_ms(500);
                                         i++;
                                         if(i == 3){i = 0;}                     //       charkhesh

                                 }
                  if(PinA4 == 1 )       {                                 //entekhabe sanye daghighe va saat be
                                                delay_ms(500);
                                            i--;
                                            if(i == -1){i = 2;}                                         //       charkhesh

                                 }
          if(PinA1 == 1 && i == 0)      {                               // pinA.1 afzayesh midahad   i=0 counter ast
                                                        delay_ms(500);
                                                counter++;
                                                    if(counter == 60){                                                   // bazgasht
                                                              counter = 0;

                                                }

                                 }
                  if(PinA2 == 1 && i == 0){                       //pinA.2 kahesh midahad
                                           delay_ms(500);
                                           counter--;
                                                 if(counter == -1){
                                                         counter = 59;


                                                }

                                 }
          if(PinA1 == 1 && i == 1)      {                                       //daghighe
                                            delay_ms(500);
                                                m++;
                                                 if(m == 60){                                                   //charkhesh
                                                 m = 0;
                                                 }
                                 }
                   if(PinA2 == 1 && i == 1){
                                           delay_ms(500);
                                           m--;
                                                if(m == -1){
                                                   m =59;

                                                }

                                 }
                    if(PinA1 == 1 && i == 2)    {                                                //saat
                                        delay_ms(500);
                                                h++;
                                 }
                        if(PinA2 == 1 && i == 2){
                                        delay_ms(500);
                                                h--;
                                                if(h == -1){
                                                h = 0;
                                                }

                                 }
                                 }

while(!flag) {
            sprintf(A,"Time: %02u:%02u:%02u ",h,m,counter);
        lcd_clear();
        lcd_putsf(A);
                      if(PinA3 == 1 && PinA4 == 1){                               //3,4 ba hm ghat mikonnand
                             break;
                                         }
                delay_ms(1000);
                counter--;                                                      //sanie km she
                  if(counter == -1 && (h!=0 || m!=0))
                                {
                                        counter = 59;
                                        m--;
                                }
                  if(m == -1 && h != 0){
                                        m = 59;
                                        h--;
                                }


                        if(counter == -1 && h == 0 && m ==0 ){
                                counter = 0;
                                sprintf(A,"Time: %02u:%02u:%02u ",h,m,counter);
                lcd_clear();
                lcd_putsf(A);
                    LED1_1;                //led 1 k b porte B.0 vasle roshan she
                delay_ms(2000);
                LED1_0;            //led 1 k b porte B.0 vasle khamoosh she
                    flag = 1;
                break;

                                }



                                        }
     }


           }


  • here is delay code:

    #ifndef __DELAY_H
    #define __DELAY_H
    static u8  fac_us=0;
    static u16 fac_ms=0;
    
    void delay_init(u8 SYSCLK)
    {
            SysTick->CTRL&=0xfffffffb;
            fac_us=SYSCLK/8;
            fac_ms=(u16)fac_us*1000;
    }
    
    void delay_ms(u16 nms)
    {
            u32 temp;
            SysTick->LOAD=(u32)nms*fac_ms;
            SysTick->VAL =0x00;
            SysTick->CTRL=0x01 ;
            do
            {
                    temp=SysTick->CTRL;
            }
            while(temp&0x01&&!(temp&(1<<16)));
            SysTick->CTRL=0x00;
            SysTick->VAL =0X00;
    }
    
    void delay_us(u32 Nus)
    {
            u32 temp;
            SysTick->LOAD=Nus*fac_us;
            SysTick->VAL=0x00;
            SysTick->CTRL=0x01 ;
            do
            {
                    temp=SysTick->CTRL;
            }
            while(temp&0x01&&!(temp&(1<<16)));
            SysTick->CTRL=0x00;
            SysTick->VAL =0X00;
    }
    #endif
    
    

  • Yes, slapping void delay_init(u8 SYSCLK); in the middle of a function body is NOT how C works.

    Perhaps the usage is delay_init(SystemCoreClock); but your question lacks any reference to the library code you're using.

    Why call SystemInit() ? isn't this already called in startup.s prior to getting to main()?

    Using the simulator or real hardware?

  • hi!thanks for your attention!
    i'm using the keil simulator!
    you can download this zip to see the whole files!

    s3.picofile.com/.../dg_timer.zip.html

  • As noted, your delay_init() will not be called so your fac_us and fac_ms variables will have very boring values. But you did notice this quickly when you debugged your code, didn't you? You did debug?

    By the way - I think most readers will be able to figure out that the variable "flag" is a flag. The $10000 question is what flag... It helps to use a variable name that tells the meaning of the variable. Think about it. If a variable gets the name "integer", people will be able to assume something about the data type. But will not be able to figure out if it represents a delay. Or a voltage. Or number of bugs ignored. Let your variable names tell a story!

  • Another thing - you have "#ifndef __DELAY_H" around your delay code.

    So you didn't just keep function prototypes in Delay.h, but decided to go for the full implementation? Clever. How do you then think you would be able to use the same delay functions from a different source file, in case your program grows? You seen much great sample code with the implementation in header files?

  • i have no idea of this stuffs i began using keil for one week
    and since then i just passed the keils errors!(more than hundred of them)and learn alittle about gpio , cfiles , hfiles , ... .
    and now after all of that im at this point what should i do now?
    i've used this code for declaration but it didn't work too.
    delay_init(72000000);