Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Problem in code to display led! Pleased help.

Status
Not open for further replies.

anhnha

Full Member level 6
Joined
Mar 8, 2012
Messages
322
Helped
4
Reputation
8
Reaction score
4
Trophy points
1,298
Activity points
3,684
Hi all,
I am programming to convert a c code section into assembly .This is may c code:
Code:
// display temperature in led
if(kt2==1)                    //
   {                             //
      output_c(0x01);            //   
      output_b(maled[t/10]);     //     display tens
   }                             //
   else                          //
   {                             //
      output_c(0x02);            //
      output_b(maled[t%10]);     //   display units
   }
and here is my ammsembly code:
Code:
#include <16f877a.h>
#device *=16 ADC=10
#use fast_io(a)
#use fast_io(b)
#use fast_io(c)
#use fast_io(d)
#use fast_io(e)
#use i2c(Master,sda=PIN_C4,scl=PIN_C3) 
#use delay(clock=20000000)
#include <lcd.c>
#bit set_ra5= 0x85.5
#byte PORTB = 0x06 
#byte TRISB = 0x86
#byte PORTC = 0x07 
#byte TRISC = 0x87 
#if kt2==1
 #byte temp_add = 0x4C
#else
 #byte temp_add = 0x4D


// led code
int8 maled[10]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};

 #asm
   MOVLW    0x00      
   MOVWF    TRISB      ;set portb is output
   MOVLW    0x18
   MOVWF    TRISC     ; set for portc
   DECFSZ 0x4F, 1     ; check condition kt2==1 or not 
   GOTO  chuc         ; go to display tens of temperature
   BSF PORTC, 1     
   GOTO  display  
chuc:
    BSF PORTC, 0
    GOTO  display     ; display units of temperature
display:
   BCF  0x03, 5
   MOVF temp_add, 0   
   XORLW  0x00
   BTFSC 0x03,2
   GOTO  zero
   MOVF   temp_add, 0
   XORLW  0x01
   BTFSC 0x03,2
   GOTO  one
   MOVF   temp_add, 0
   XORLW  0x02
   BTFSC 0x03,2
   GOTO  two
   MOVF   temp_add, 0
   XORLW  0x03
   BTFSC 0x03,2
   GOTO  three
   MOVF   temp_add, 0
   XORLW  0x04
   BTFSC 0x03,2
   GOTO  four
   MOVF   temp_add, 0
   XORLW  0x05
   BTFSC 0x03,2
   GOTO  five
   MOVF   temp_add, 0
   XORLW  0x06
   BTFSC 0x03,2
   GOTO  six
   MOVF   temp_add, 0
   XORLW  0x07
   BTFSC 0x03,2
   GOTO  seven
   MOVF   temp_add, 0
   XORLW  0x08
   BTFSC 0x03,2
   GOTO  eight
   MOVF   temp_add, 0
   XORLW  0x09
   BTFSC 0x03,2
   GOTO  nine
   GOTO end_1
 
zero: MOVLW 0xC0
       MOVWF PORTB
       GOTO end_1 
one:   MOVLW 0xF9
       MOVWF PORTB
       GOTO end_1
two:   MOVLW 0xA4
       MOVWF PORTB
       GOTO end_1
three: MOVLW 0xB0
       MOVWF PORTB
       GOTO end_1
four:  MOVLW 0x99
       MOVWF PORTB
       GOTO end_1       
five:   MOVLW 0x92
       MOVWF PORTB
       GOTO end_1       
six:   MOVLW 0x82
       MOVWF PORTB
       GOTO end_1       
seven: MOVLW 0xF8
       MOVWF PORTB
       GOTO end_1       
eight: MOVLW 0x80
       MOVWF PORTB
       GOTO end_1       
nine:  MOVLW 0x90
       MOVWF PORTB
       GOTO end_1       
end_1: 
 #endasm
When i build and run it in proteus then is only the unit of temperature display, it can not display the tens.I had try to find how compiler CCS compile it but i don't understand why i can't display the tens of temperature.Would you give the explaination of that is wrong in this code?
Thank you.
 

1) Where do you clear PORTC,0 and PORTC,1? Is it somewhere else in the program?
I see them set but not cleared:

Code ASM - [expand]
1
2
3
4
5
BSF PORTC, 1     
   GOTO  display  
chuc:
    BSF PORTC, 0
    GOTO  display     ; display units of temperature


2) The display lookup routine <display: to end1:> is only defined for values from 0-9. If you have a temperature above 99, t/10 will be over 9 and nothing will be displayed. Two fixes: the quick one is add the following lines:
change this:

Code ASM - [expand]
1
2
3
4
XORLW  0x09
   BTFSC 0x03,2
   GOTO  nine
   GOTO end_1



to:

Code ASM - [expand]
1
2
3
4
5
XORLW  0x09
   BTFSC 0x03,2
   GOTO  nine
   GOTO  above_nine
   GOTO end_1



and change this:

Code ASM - [expand]
1
2
3
4
nine:  MOVLW 0x90
       MOVWF PORTB
       GOTO end_1       
end_1:



to:

Code ASM - [expand]
1
2
3
4
5
6
7
nine:  MOVLW 0x90
       MOVWF PORTB
       GOTO end_1 
above_nine:  MOVLW 0xdf
       MOVWF PORTB
       GOTO end_1       
end_1:




This will display a "-" sign in the tens digit if t > 99.


The other way to fix it is to create the hexadecimal numbers "A,B,C,D,E, and F" in the led table and make the tests for 10,11,12,13,14, and 15, like the test already in the assembly for 0 through 9. Then you will know exactly what your temperature is: "B7" = 117 degrees (B*10+7).
One last comment: if your temperature is over 159 degrees, then the tens digit will explode too, since 165/10 = 16 which can't be displayed.
 
Last edited by a moderator:

Thanks.but this is all my code:
Code:
#include <16f877a.h>
#device *=16 ADC=10
#use fast_io(a)
#use fast_io(b)
#use fast_io(c)
#use fast_io(d)
#use fast_io(e)
#use i2c(Master,sda=PIN_C4,scl=PIN_C3) 
#use delay(clock=20000000)
#include <lcd.c>
#bit set_ra5= 0x85.5
#byte PORTB = 0x06 
#byte TRISB = 0x86
#byte PORTC = 0x07 
#byte TRISC = 0x87 


#if kt2==1
 #byte temp_add = 0x4C
#else
 #byte temp_add = 0x4D



int8 maled[10]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};


int8 sec=0,min=0,hour=0,day=0,date=0 ,month=0,year=0;
int8 sec1=0,sec2=0,min1=0,min2=0,hour1=0,hour2=0,date1=0,date2=0,month1=0,month2=0,year1=0,year2=0;


int8 count1=0,count2,count_button3=0,count_button2=0,count_button1=0,count_button4=0;


int16 t;
int8 t1, t2;

int8 kt1=0,kt2=0,kt3=0,kt4=0,kt5=0;

int8 data1=0,data2=0,set=0;

void update_ds1307(void);
void update_time(void);
void lcd_time(void);
void quet_led(int16 t);
void bcd_to_7seg(int number);
void display_set_time(int8 set);
void set_time(int8 set);


#int_timer0
void ngat_timer0()
{   
   if(input(PIN_a5) && (count_button3>=15))
   {
      ++set;
      if(set==7) set=0;
      count_button3=0;
   }
   if(input(PIN_e0) && (count_button2>=15)&& set)
   {
      kt4=1;
      count_button2=0;
   }
    if(input(PIN_e1) && (count_button4>=15)&& set)
   {
      kt5=1;
      count_button4=0;
   }
   if(input(PIN_e2) && (count_button1>=15) && set)
   {
      set=0;
      update_ds1307();
      count_button1=0;
   }
}
#int_timer1                         
void ngat_timer1()
{  
   count1++;                   
   count2++;
   count_button3++;
   count_button2++;
   count_button4++;
   count_button1++;
   kt2++; 
 
   #asm
   MOVLW    0x00
   MOVWF    TRISB
   MOVLW    0x18
   MOVWF    TRISC 
   DECFSZ 0x4F, 1   
   GOTO  chuc
   BSF PORTC, 1
   GOTO  display  
chuc:
    BSF PORTC, 0
    GOTO  display
display:
   BCF  0x03, 5
   MOVF temp_add, 0
   XORLW  0x00
   BTFSC 0x03,2
   GOTO  zero
   MOVF   temp_add, 0
   XORLW  0x01
   BTFSC 0x03,2
   GOTO  one
   MOVF   temp_add, 0
   XORLW  0x02
   BTFSC 0x03,2
   GOTO  two
   MOVF   temp_add, 0
   XORLW  0x03
   BTFSC 0x03,2
   GOTO  three
   MOVF   temp_add, 0
   XORLW  0x04
   BTFSC 0x03,2
   GOTO  four
   MOVF   temp_add, 0
   XORLW  0x05
   BTFSC 0x03,2
   GOTO  five
   MOVF   temp_add, 0
   XORLW  0x06
   BTFSC 0x03,2
   GOTO  six
   MOVF   temp_add, 0
   XORLW  0x07
   BTFSC 0x03,2
   GOTO  seven
   MOVF   temp_add, 0
   XORLW  0x08
   BTFSC 0x03,2
   GOTO  eight
   MOVF   temp_add, 0
   XORLW  0x09
   BTFSC 0x03,2
   GOTO  nine
   GOTO end_1
 
zero: MOVLW 0xC0
       MOVWF PORTB
       GOTO end_1 
one:   MOVLW 0xF9
       MOVWF PORTB
       GOTO end_1
two:   MOVLW 0xA4
       MOVWF PORTB
       GOTO end_1
three: MOVLW 0xB0
       MOVWF PORTB
       GOTO end_1
four:  MOVLW 0x99
       MOVWF PORTB
       GOTO end_1       
five:   MOVLW 0x92
       MOVWF PORTB
       GOTO end_1       
six:   MOVLW 0x82
       MOVWF PORTB
       GOTO end_1       
seven: MOVLW 0xF8
       MOVWF PORTB
       GOTO end_1       
eight: MOVLW 0x80
       MOVWF PORTB
       GOTO end_1       
nine:  MOVLW 0x90
       MOVWF PORTB
       GOTO end_1       
end_1: 
 #endasm            

   
   if(count1==25)      
   {
      if(set==0) 
      update_time();          
      t=read_adc();            
      t=t*100/204;
      t1=t/10;
      t2=t%10;
      count1=0;
      kt1=1;
   }
   if(count2==10)
   {
      kt3++;
      count2=0;
   }
 
   set_timer1(15535);
}
void main() 
{
 
   lcd_init();
   
 
   set_ra5=1;
   
   
   set_tris_e(0b111);
  output_e(0);
   set_tris_b(0b10000000);
   output_b(0);
   set_tris_c(0b11100100);
   output_c(0);
   
   //setup adc
   setup_adc_ports(AN0);
   setup_adc(adc_clock_internal);
   set_adc_channel(PIN_A0);
   
   //setup timer0
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256);
   set_timer0(0);
   enable_interrupts(int_timer0);
   
   //setup timer1
   enable_interrupts(INT_TIMER1);            
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_2); 
   set_timer1(15535);

  
   enable_interrupts(global);
   //
   while(true)
   {
      if((kt1==1) && (set==0))        
      {                   //
         lcd_time();      //
         kt1=0;           //
      }//
      if(set!=0)
      {
         display_set_time(set);
         set_time(set);
      }
   }
}
void update_ds1307(void)              
{
    int8 data;
    i2c_start();  
    i2c_write(0xd0);  
    i2c_write(0x00);                
 
    data=sec1+(sec2<<4);             //
    data=data & 0b01111111;          //
    i2c_write(data);                 // set giay
 
    data=min1+(min2<<4);             //
    i2c_write(data);                 
 
    data=hour1+(hour2<<4) ;          //
    i2c_write(data);              
 
    data=day;                        //
    i2c_write(data);                
 
    data=date1+(date2<<4);           //
    i2c_write(data);             
 
    data=month1+(month2<<4);         //
    i2c_write(data);                
 
    data=year1+(year2<<4);           //
    i2c_write(data);               
 
    data=0x10;                       //
    i2c_write(data);                 //set control
 
    i2c_stop();                    
}
void update_time() 
{  
    i2c_start();
    i2c_write(0xD0);
    i2c_write(0x00); 
    i2c_stop();
    I2C_start();
    I2C_write(0xD1);
    sec   = i2c_read(1); 
    min   = i2c_read(1);  
    hour  = i2c_read(1);
    day   = i2c_read (1);  
    date  = i2c_read(1);  
    month = i2c_read(1); 
    year  = i2c_read(0);  
    i2c_stop();
    sec1=sec & 0x0F; 
    sec2=(sec & 0x70)>>4;       //convert to BCD SEC  
    min1=min & 0x0F;  
    min2=(min & 0x70)>>4;       //convert to BCD MIN
    hour1=hour & 0x0F; 
    hour2=(hour & 0x30)>>4;     //convert to BCD HOUR  
    day=(day & 0x07);  
    date1=date & 0x0F; 
    date2=(date & 0x30)>>4;     //convert to BCD DATE 
    month1=month & 0x0F; 
    month2=(month & 0x10)>>4;   //convert to BCD MONTH 
    year1= year & 0x0F; 
    year2=(year & 0xF0)>>4;     //convert to BCD YEAR  
}

void lcd_time()                 
{
   lcd_gotoxy(5,1);
   printf(lcd_putc,"%u",date2);
   printf(lcd_putc,"%u",date1);
   lcd_putc('-');
   printf(lcd_putc,"%u",month2);
   printf(lcd_putc,"%u",month1);
   lcd_putc('-');
   printf(lcd_putc,"%u",year2);
   printf(lcd_putc,"%u",year1);
   lcd_putc("    ");
   lcd_gotoxy(5,2);
   printf(lcd_putc,"%u",hour2);
   printf(lcd_putc,"%u",hour1);
   lcd_putc(':');
   printf(lcd_putc,"%u",min2);
   printf(lcd_putc,"%u",min1);
   lcd_putc(':');
   printf(lcd_putc,"%u",sec2);
   printf(lcd_putc,"%u",sec1);
   lcd_putc("     ");
}
void display_set_time(int8 set)
{
   switch(set)
   {
      case 6:
      {
         if(kt3==0)
         {
            lcd_gotoxy(8,1);
            printf(lcd_putc,"%u",month2);
            printf(lcd_putc,"%u",month1);
            lcd_putc('-');
            printf(lcd_putc,"%u",year2);
            printf(lcd_putc,"%u",year1);
            lcd_putc("    ");
            break;
         }
         else
         {
            lcd_gotoxy(11,1);
            lcd_putc("      ");
            break;
         }
      }
      case 5:
      {
         if(kt3==0)
         {
            lcd_gotoxy(5,1);
            printf(lcd_putc,"%u",date2);
            printf(lcd_putc,"%u",date1);
            lcd_putc('-');
            printf(lcd_putc,"%u",month2);
            printf(lcd_putc,"%u",month1);
            lcd_putc('-');
            break;
         }
         else
         {
            lcd_gotoxy(8,1);
            lcd_putc("  -");
            break;
         }
      }
      case 4:
      { 
         if(kt3==0)
         {
            lcd_gotoxy(5,1);
            printf(lcd_putc,"%u",date2);
            printf(lcd_putc,"%u",date1);
            lcd_putc('-');
            lcd_gotoxy(5,2);
            printf(lcd_putc,"%u",hour2);
            printf(lcd_putc,"%u",hour1);
            break;
         } 
         else
         {
            lcd_gotoxy(5,1);
            lcd_putc("  -");
            break;
         }
      }
      case 3:
      {
         if(kt3==0)
         {
            lcd_gotoxy(5,2);
            printf(lcd_putc,"%u",hour2);
            printf(lcd_putc,"%u",hour1);
            lcd_putc(':');
            printf(lcd_putc,"%u",min2);
            printf(lcd_putc,"%u",min1);
            break;
         } 
         else
         {
            lcd_gotoxy(5,2);
            lcd_putc("  :");
            break;
         } 
      }
      case 2:
      {
         if(kt3==0)
         {
            lcd_gotoxy(8,2);
            printf(lcd_putc,"%u",min2);
            printf(lcd_putc,"%u",min1);
            lcd_putc(':');
            printf(lcd_putc,"%u",sec2);
            printf(lcd_putc,"%u",sec1);
            break;
         }
         else
         {
            lcd_gotoxy(8,2);
            lcd_putc("  :");
            break;
         }
      }
      case 1:
      {
         if(kt3==0)
         {
            lcd_gotoxy(11,2);
            printf(lcd_putc,"%u",sec2);
            printf(lcd_putc,"%u",sec1);
            lcd_putc("     ");
            break;
         } 
         else
         {
            lcd_gotoxy(11,2);
            lcd_putc("      ");
            break;
         }
      }
   }  
}
void set_time(int8 set)
{
   switch(set)
   {
      case 6:
      {
         if   (kt4==1){
         year1++;kt4=0;
         if ((year2==9)&(year1==10)) {year1=year2=0;break;}  
         if (year1==10) {year1=0;year2++;break;}
         break;}
        
         
         
         if(kt5==1) {
         year1--;
         kt5=0;
         if ((year2==0)&(year1==-1)) {year1=year2=9;break;}  
         if ((year1==-1)&(year2!=0)) {year1=9;year2--;break;}
          
          break; }
      }
      case 5:
      {
         if(kt4==1) {month1++;kt4=0;     // if(kt5==1) { month1--;kt5=0;}
         if ((month2==1) & (month1==3)) { month2=0;month1=1;break;}  
         if (month1==10) { month2++;month1=0;break;}}
         if(kt5==1) { month1--;kt5=0;
          if ((month2!=0) & (month1==-1)) { month2--;month1=9;break;}  
         if ((month1==0)&(month2==0))
         { month2=1;month1=2;break;
         }
         }}
      case 4:
      {
         if(kt4==1){date1++; kt4=0;
         //if(kt5==1){date1--; kt5=0;}{
         if((date2==3) & (date1==2)) { date2=0;date1=1;break;} 
         if(date1==10) { date2++;date1=0;break;}} 
         if(kt5==1){date1--; kt5=0;
         if((date2==0) & (date1==0)) { date2=3;date1=1;break;} 
         if((date1==-1)&(date2!=0 )) { date2--;date1=9;break;}
         }}
      case 3:
      {
         if(kt4==1){hour1++;kt4=0;
         if((hour2==2) & (hour1==4)) { hour2=0;hour1=0;break;}  
         if(hour1==10) { hour2++;hour1=0;break;} }
          if(kt5==1){hour1--;kt5=0;
          if((hour2==0) & (hour1==-1)) { hour2=2;hour1=4;break;}  
         if((hour1==-1)&(hour2!=0)) { hour2--;hour1=9;break;}
          }}
      case 2:
      {
         if(kt4==1) { min1++;kt4=0;
         if ((min2==5) & (min1==10)) { min2=0;min1=0;break;} 
         if (min1==10) { min2++;min1=0;break;}}
          if(kt5==1) { min1--;kt5=0;
            if ((min2==0) & (min1==-1)) { min2=5;min1=9;break;} 
         if ((min1==-1)&(min2!=0)) { min2--;min1=9;break;}}
         
      }
      case 1:
      {
         if(kt4==1){sec1++;kt4=0;
         if ((sec2==5) & (sec1==10)) { sec2=0;sec1=0;break;} 
         if (sec1==10) { sec2++;sec1=0;break;}}
         if(kt5==1){sec1--;kt5=0; 
          if ((sec2==0) & (sec1==-1)) { sec2=5;sec1=9;break;} 
         if ((sec1==-1)&(sec2!=0)) { sec2--;sec1=9;break;}
      }
   }
      }}
In the project i only want to display a temperature between 00 to 99 and i use to 7 segments.I want to read temperature from ADC and dispaly it but only unit digit is dislayed?
 

in the line:
DECFSZ 0x4F, 1
what is stored on address 0x4f? where does it inited?
 

this is address of kt2 var.I want to check if kt2==1 then display led. it is display after an interval of time
 

is kt2 always located at 0x4f? i could not find any declaration of it with this address.
what is the meaning of
#if kt2==1
#byte temp_add = 0x4C
#else
#byte temp_add = 0x4D

before kt2 was declared?
 

is kt2 always located at 0x4f? i could not find any declaration of it with this address.
what is the meaning of
#if kt2==1
#byte temp_add = 0x4C
#else
#byte temp_add = 0x4D

before kt2 was declared?
is kt2 always located at 0x4f?
yes.it is always located at 0x4f.
i could not find any declaration of it with this address
I have to use assembly for kt2 in this section.therefore i have use list file in CCS to find its address.
what is the meaning of
#if kt2==1
#byte temp_add = 0x4C
#else
#byte temp_add = 0x4D

before kt2 was declared?
Yes.
0x4C is address of file register that store a unit digit of temperature and 0x4D is address of file register that store a ten digit of temperature.I have to display a temperature with 2 digit in 7 segment led, because the code for display ten and unit digit is very similar.it is different at the address of ten or digit where it is store.For this reason i want to use this code to display for ten and unit digit I do not want to write another code that is very similar like that.in the above code if kt2 is equal to 1 the address is read is 0x4C and a unit digit will be display, otherwise a 0x4D is read and ten digit will be display.
 

in the line:
DECFSZ 0x4F, 1
what is stored on address 0x4f? where does it inited?

Here's anhnha's initialization:
Code:
int8 kt1=0,kt2=0,kt3=0,kt4=0,kt5=0;


---------- Post added at 23:51 ---------- Previous post was at 23:45 ----------

Anhnha-
Did you check to make sure that your temperature is not larger than 99? Did you implement the easy code changes I gave you in Post #2? Do you see a "-" or still nothing at all in the 10's digit with the "above_nine" changes?
 

if i am not wrong the #if is only evaluated on compilation, therefore temp_add will alwayes have the same value, no matter what the value of kt2 was changed while running
 
  • Like
Reactions: anhnha

    anhnha

    Points: 2
    Helpful Answer Positive Rating
Did you check to make sure that your temperature is not larger than 99? Did you implement the easy code changes I gave you in Post #2? Do you see a "-" or still nothing at all in the 10's digit with the "above_nine" changes?
Thanks.I have stimulated it in proteus and i used LM35 sensor therefor the temperature is always lower than 100.I think that my problem is that the #if kt2==1
#byte temp_add = 0x4C
#else
#byte temp_add = 0x4D
I found it in ccs compiler pre-processor but it seem not work? the temp_add is not change no matter kt2.Would you give me the way to do replace this pre-processor?

---------- Post added at 15:51 ---------- Previous post was at 15:47 ----------

if i am not wrong the #if is only evaluated on compilation, therefore temp_add will alwayes have the same value, no matter what the value of kt2 was changed while running
Yes.You are right but i do not understand why this is happen?I use ccs compiler and it have a pre-processor like this:
Code:
#IFDEF
#IFNDEF 
#ELSE
#ELIF
#ENDIF
Syntax:
 #ifdef  id

   code

#elif

   code

#else

   code

#endif

 

#ifndef id

   code

#elif

   code

#else

   code

#endif

 
 
Elements:
 id is a preprocessor identifier, code is valid C source code.

 
 
Purpose:
 This directive acts much like the #IF except that the preprocessor simply checks to see if the specified ID is known to the preprocessor (created with a #DEFINE). #IFDEF checks to see if defined and #IFNDEF checks to see if it is not defined. 

 
 
Examples:
 #define debug     // Comment line out for no debug

 

...

#ifdef  DEBUG

printf("debug point a");

#endif
Do you know how to do this to instead of the pre-processor?
Thanks.
 

you can add at the begging of your #asm block this:
movlw 0x4C
btfss kt2, 1
movlw 0x4D
movwf temp_add, 1
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top