Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

8051 Atmel programming in C- Need help for one example

Status
Not open for further replies.

8051help

New Member
I need to write only numbers (every second) in string on display (memory mapped on 0x8001 adress) which are divding with 2 .

I must use counter not delay () for interrupt ...


Below is code which i imagine but dont work correctly...
C:
#include <reg51.h>
#include <math.h>
typedef unsigned char byte;
byte a[3]= {2,1,8};

byte counter, frequency,displ;
int i=0;
byte xdata display _at_ 0x8001;

sbit switcher=P0^0;

void Inic(void) {
   EA=1;
   ET0=1;
   TMOD=1;
   TH0=0x3C;
   TL0=0xB0;
   TR0=1;
   counter=1;
   frequency=0;
}

void timer0(void) interrupt 1 using 2 {
   TR0=0;
   TH0=0x3C;
   TL0=0xB0;
   TR0=1;
   if(switcher)  {
     if(!(--counter)) {
       counter=frequency;
         display=displ;  
     }
   }   
}

void main(void) {
   Inic();
   while(1) {
     if(switcher) {  
       for (i=0;i<3;i++)  {
         if(a/2)  {
           displ=a;   
           frequency=20;
          }
       }   
     }  
   }
}
 
Last edited by a moderator:
**Please use coding tags** Makes it easier to read...

I've done a few tutorials using 8051
https://www.electro-tech-online.com/articles/authors/ian-rogers.163748/

About your code..... Where is 'a' defined / initialized ??? Do you mean
C:
void main(void) {
   Inic();
   while(1) {
     if(switcher) {  
       for (i=0;i<3;i++)  {
         if(a[i]/2)  {
           displ=a[i];  
           frequency=20;
          }
       }  
     }  
   }
}
 
okey, THANK YOU but that dont work in keil softwer
something is wrong. I need to write printing in interrupt part (where is if (!(--counter) itc...)
. So I need printing on display every second.
I have books but they dont wont to tell me what can i do in that situation :D...
I can find mistake in that code .
 
I tried to define timer (lower and higher bit ) on 50ms. In hexa code is 3CB0. So, I need one second . I will count that in way to use counter with 20 interrrupes because 20*50ms = one second. ANd that part is clear but I am not sure that is goood defined in my code rutine.
I must use counter not delay ()
 
OTHER WAY that i tried is below. I trie to put mathematical operation in interrupt part and frequency only in main part
Code:
#include <reg51.h>
#include <math.h>
typedef unsigned char byte;
byte a[3]= {2,1,8};

byte counter, frequency;
int i=0;
byte xdata display _at_ 0x8001;

sbit switcher=P0^0;

void Inic(void) {
   EA=1;
   ET0=1;
   TMOD=1;
   TH0=0x3C;
   TL0=0xB0;
   TR0=1;
  counter=1;
   frequency=0;
}

void timer0(void) interrupt 1 using 2 {
   TR0=0;
   TH0=0x3C;
   TL0=0xB0;
   TR0=1;

if(switcher)   {
   if(!(--counter)) {
   counter=frequency;
         
     
      for (i=0;i<3;i++)   {
    if(a[i]/2)     {
       display=a[i];   
       }}    }  }    }        

void main(void) {
   Inic();
   while(1) {
      if(switcher)   {
     
     frequency=20;
   
   }  }    }
 
AHH!! You need to write every second, not every second character...
You don't need interrupts to use the timer.. The overflow flag will still be available.

The timer must be loaded with 0x3CB0 ( as you have done ) and a total of 20 counts..
So
C:
counter = 20;
while(counter--)   // while count of 20
  {   
   TH0=0x3C;
   TL0=0xB0;      // set timer 50ms
   TF0 = 0;          // Clear overflow flag
   TR0 = 1;          // Run timer
   while(! TF0);    // untill overflow
   TR0=0;          // clear timer...
  }

This is a 1 second delay so write your display after the while statement...
 
Great, but we never use while(counter--). We always use if counter something in bracket - plus or minus or something else...
Can I use if (counter --), itc..? And can I keep frequency and other...?

Code:
if(counter--) {
  counter=frequency;
   
   
  for (i=0;i<3;i++)  {
  if(a[i]/2)  {
  display=a[i];  
  }}  }  }  }       

void main(void) {
   Inic();
   while(1) {
      if(switcher)   {
     
     frequency=20;
   
   }  }    }
 
Yes , I have to use interrupt.
Code looks just like I wrote first. But something is wrong.
I know that I must put printing on display in interrupt rutine but what that code do I think that I must put in main rutine. For example, if is everything in interrupt rutine what would be in main ??:). So, that is confused. If I write everything in main , I have to find way to connect printing with interrupt rutine to print on display...Understand what i mean? :D

Program isn t complicated but have a trick, apparently.
 
The real example of this problem is four string with with 16 numbers and I have to put on display only numbers which are divided with 3.
But I trying to solving this easier problem, with one string an less numbers..
 
Take a look at this I threw this together.... It is a one second timer setup ( using the interrupt... SDCC uses a slightly different wording ) and steps through the array.... It should be a base for you!!

C:
#include<8051.h>
unsigned char a[] = {1,2,4,8,16,32,64,128};

volatile int isr_count = 0, frequency=0;

void isr(void) __interrupt(1)
  {
  TH0 = 0x3C;
  TL0 = 0xB0;
  if(isr_count++ > 19)
     {
     frequency = 1;
     isr_count = 0;
     TR0 = 0;
     }
  TF0 = 0;
  }

void main(void)
  {
  int x=0;
  TMOD = 1;
  ET0 = 1;
  EA = 1;
  while(1)
     {
     TH0 = 0x3C;
     TL0 = 0xB0;
     TR0 = 1;
     while(!frequency);
     frequency = 0;
     P0 = a[x];
     if(x++ == 7) x=0;
     }
  }
 
I tried like below . Dividing is now correct but i still have problem with time of 1s (i need 1s per number on display). Still too fast changing of numbers on display

Code:
#include <reg51.h>
#include <math.h>   
typedef unsigned char byte;

byte a[9]= {3,7,5,16,1,4,11,9,6};

byte counter;
byte xdata display _at_ 0x8001;
byte started = 0;
int i=0;

sbit switcher=P0^0;
void Inic(void)
{
  EA=1;   
  TMOD=1;  
  TH0=0xC3;
  TL0=0x50; 
  TR0=1;
  counter=1; 
}

void timer0(void) interrupt 1 using 2
{   
  TH0=0xC3;
  TL0=0x50; 
   
  counter--;  /
  if(counter == 0)  
  {
  counter = 20;   
  if(started == 1)  
  {
  if(i < 9)       
  if(!(a[i]%3) )   
  display=a[i];             
  i++;  /
  if(i == 9) 
  ET0 = 0;  
  TR0 = 0;  
  started = 0; 
  }
  }
  }
}

void main(void)
{
  Inic();   
  switcher = 1;  
   
  while(1)   
  {   
  if(switcher == 0)   
  if(started == 0)   
  {   
  started = 1;  
  i = 0;  //
  counter = 20;  
  //
  TH0=0xC3;  
  TL0=0x50;  
  ET0=1;  
  TR0=1;  
  }
  }   
}
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top