trying to make a program using C

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

trying to make a program using C

1,236 Views
Santanafx
Contributor I

Hi everyone, im trying to make the same program from assembly to C

By the way im using the microcontroler MC68HC908QY4

 

My program in assembly is this.

 


DELAY: DS 1
MyCode:     SECTION
main:
_Startup:

CONFIGURATIONS:
                  LDA  #$19
                  STA CONFIG1 ;no COP; no LVI; +5V
                  LDA #0
                  STA CONFIG2   ;3,2MHZ ; no extern interruption; no RESET;
                  LDA #$38
                  STA DDRA    ;PTA5 , PTA4 E PTA3 outputs
INI:
GREEN:
              LDA #$20
              STA PTA   ;turn on
            
              BSR D1SEC
YELLOW:
                    LDA #$10
                    STA PTA   ; turn on
      
                     BSR D1SEC
RED:
                        LDA #$08
                        STA PTA  ;turn on 
                        
                         BSR D1SEC
                       
                       
                        BRA INI
                       


     

             
D1SEC:
              LDA #100
              STA DELAY    
D10MILI:
                LDX #100
D100MICRO:
                      LDA #80
 LOOP:
              DECA
              BNE LOOP
                DECX
                BNE D100MICRO 
                DEC DELAY

                BNE D10MILI
              RTS
             
              END
                  

 


This program uses three LEDs when choosing each output (PTA3, PTA4, PTA5), the LED is turned on, for one second, the loop D1SEC, creates a delay of 1 second.

 

Here is the same program but with two loops now, each loop will make the leds be turned on with diferent times. There are some minor changes now, but the program as i said is the same. The program with assembly is working fine!

 

 

            INCLUDE 'derivative.inc'

            XDEF _Startup, main
   

MY_ZEROPAGE: SECTION  SHORT       

TIMESIGNAL: DS 1
DELAY: DS 2
MyCode:     SECTION
main:
_Startup:

CONFIGURATIONS:


                  MOV #$19, CONFIG1

                  MOV #0, CONFIG2

                  MOV #$38, DDRA  
                  MOV #$1, PTAPUE
INI:
READKEY:
          LDA PTA ;A=X X GREEN   YELLOW   RED   X X CH0
          AND #1; #1=0 0        0              0              0      0 0   1
          BNE  TEMP1 ;A=0000000CH0   

TEMP0:
GREEN:

              MOV #$20, PTA

              MOV #5,TIMESIGNAL ;5SEC
LGREEN:
             
              BSR D1SEG
              DEC TIMESIGNAL
              BNE LGREEN
             
            
YELLOW:

                    MOV #$10, PTA
                    MOV #2, TIMESIGNAL ;2SEC
LYELLOW:
             
              BSR D1SEC
              DEC TIMESIGNAL
              BNE LYELLOW
             
RED:

                        MOV #$08, PTA
                       MOV #4, TIMESIGNAL ;4SEC
LRED:
             
              BSR D1SEC
              DEC TIMESIGNAL
              BNE LRED
              
                       
                       
                        BRA INI 
                       
TEMP1:
GREEN1:

              MOV #$20, PTA 

              MOV #4,TIMESIGNAL ;4SEC
LGREEN1:
             
              BSR D1SEC
              DEC TIMESIGNAL
              BNE LGREEN1
             
            
YELLOW1:

                    MOV #$10, PTA

                    MOV #1, TIMESIGNAL ;1SEC
LYELLOW1:
             
              BSR D1SEC
              DEC TIMESIGNAL
              BNE LYELLOW1
             
RED1:

                        MOV #$08, PTA
                       MOV #2, TIMESIGNAL ;2SEC
LRED1:
             
              BSR D1SEC
              DEC TIMESIGNAL
              BNE LRED1
              
                       
                       
                        BRA INI
                       

     

             
D1SEC:
               MOV #100, DELAY+1

               
D10MILI:
                 MOV #100, DELAY
D100MICRO:
                      LDA #80
 LOOP:
              DECA
              BNE LOOP
                DEC DELAY
                BNE D100MICRO 
                DEC DELAY+1
                BNE D10MILI
             
              RTS
             
              END

 

 

And now here is my problem, when im trying to make the same code (using the code with 2 loops), it gives me an error in line 19)

 

#include <hidef.h> /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */
#define RED 0x08
#define GREEN 0x20
#define YELLOW 0x10

#define DLY5 40000
#define DLY2 16000
#define DLY4 32000
#define DLY1 8000
 
void temporiza( unsigned long int);
void main(void)
{
  CONFIG1=0x19;
  CONFIG2=0x00;
  DDRA=0x38;
  PTAPUE=0x01;
}
if(!PTA_ PTA0)                          // MY PROBLEM IS IN THIS LINE
{
for( ;; )
{
PTA=GREEN;
temporiza(DLY5);
PTA=YELLOW;
temporiza(DLY2);
PTA=RED;
temporiza(DLY4);
}
else
{
PTA=GREEN;
temporiza(DLY4);
PTA=YELLOW;
temporiza(DLY1);
PTA=RED;
temporiza(DLY2);
}
}
void temporiza( unsigned long int delay)
{
while(delay--);
}

 

 

Im trying to make my if works.

 

Someone can help me please?

 

Thanks a lot for your time.

Labels (1)
Tags (1)
0 Kudos
Reply
3 Replies

987 Views
bigmac
Specialist III

Hello, and welcome to the forum.

 

Firstly, you have some major structural problems with main() function.

 

void main(void){   CONFIG1 = 0x19;   CONFIG2 = 0x00;   DDRA    = 0x38;   PTAPUE  = 0x01;}   if (!PTA_ PTA0) {      // MY PROBLEM IS IN THIS LINE   for ( ; ; ) {            PTA = GREEN;      temporiza( DLY5);      PTA = YELLOW;      temporiza( DLY2);      PTA = RED;      temporiza( DLY4);   }   else {      PTA=GREEN;      temporiza( DLY4);      PTA=YELLOW;      temporiza( DLY1);      PTA=RED;      temporiza( DLY2);   }}

 

The function exits immediately after the initialisation.  The main() function must never exit.  The remainder of the code is dead.

Had the function not exited, you would have been entering an infinite loop if the input pin was active high - not what you intended.

 

Had you created the project using the new project wizard, these problems would not have occurred because a framework is created for main(), to assist in obtaining lthe correct format.  Your code might also take into accout that the project might eventually use interrupts, and that the COP timer would be active.  The framework code also takes this into account.

 

 

Here is the corrected version of your code.

 

void main(void){   CONFIG1 = 0x19;   CONFIG2 = 0x00;   DDRA    = 0x38;   PTAPUE  = 0x01;   for ( ; ; ) {      // Main loop            if (!PTA_ PTA0) {         PTA = GREEN;         temporiza( DLY5);         PTA = YELLOW;         temporiza( DLY2);         PTA = RED;         temporiza( DLY4);      }      else {         PTA = GREEN;         temporiza( DLY4);         PTA = YELLOW;         temporiza( DLY1);         PTA = RED;         temporiza( DLY2);      }   }}

 

With such long delays generated by the delay function, the COP timer should be periodically cleared within the delay loop, to allow for occasions when the COP timer is enabled.  This extra code will affect the calibration of lthe delay loop.

 

A far better way of generating long time delays is to make use of the TIM module and the TIM overflow interrupt.  Simply initialise a global counter variable to the delay required.  Then, within the TIM overflow interrupt function, test the counter value for non-zero, and if so decrement the counter. 

 

if (TIMcount)  TIMcount--;

 

When the counter value reaches zero, the required delay has expired.

 

volatile unsigned int TIMcount;  // Global variable// Delay by multiple of TIM overflow periodvoid delay( unsigned int val){   TIMcount = val;   while (TIMcount)  __RESET_WATCHDOG();}

  

Regards,

Mac

 

0 Kudos
Reply

987 Views
Santanafx
Contributor I

Thanks a lot for your help bigmac, it worked fine now!!! Such a terrible mistake... Anyway thanks a lot!!!

 

I have another simple question, i have to make this same program with 4 diferent loops. In assembly i manage to make this doing a minor change, the program now uses

 

_Startup:CONFIGURATIONS:                                    MOV #$19, CONFIG1                   MOV #0, CONFIG2                       MOV #$38, DDRA                     MOV #$3, PTAPUE INI:LERCHAVE:          LDA PTA ;A=  X   X    GREEN     YELLOW           RED         X    CH1  CH0          AND #3; #1=  0   0       0       0                 0        0    1      1          BEQ  TEMP0 ;A=000000CH1CH0   CH1CH0=00          DECA          BEQ TEMP1          DECA          BEQ TEMP2TEMP3:                      MOV #$20, PTA                          BRA INITEMP2:                      MOV #$10, PTA                      BSR D1SEC                      MOV #0,PTA                      BSR D1SEC                      BRA INI

 

TEMP0,TEMP1,PISCAAMARELO,FIXAVERDE. Are my diferents loops. The program above dont have the loops, TEMP0 and TEMP1, they are the same as before.

 

How can i manage to do this in C?

 

Thanks a lot

0 Kudos
Reply

987 Views
bigmac
Specialist III

Hello,

 

The simplest way of handling more than two input states would be to use a switch statement ...

 

#define OFF  0x00...void main( void){   ...   for ( ; ; ) {             // Main loop            switch (PTA & 0x03) {  // Input range 0-3      case 0:         PTA = GREEN;         temporiza( DLY5);         PTA = YELLOW;         temporiza( DLY2);         PTA = RED;         temporiza( DLY4);         break;      case 1:         PTA = GREEN;         temporiza( DLY4);         PTA = YELLOW;         temporiza( DLY1);         PTA = RED;         temporiza( DLY2);         break;      case 2:         PTA = YELLOW;         temporiza( DLY1);         PTA = OFF;         temporiza( DLY1);         break;      default:   // case 3:         PTA = GREEN;      }   }}

 

Regards,

Mac

 

0 Kudos
Reply