Connect with us

Atmel 89C51 programming in C example

Discussion in 'Microcontrollers, Programming and IoT' started by 8051Help, Nov 27, 2015.

  1. 8051Help

    8051Help

    2
    0
    Nov 27, 2015
    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 ...

    #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;

    } } } } }
     
  2. Anon_LG

    Anon_LG

    453
    117
    Jun 24, 2014
    Is this a question or an example? I will assume a question. I order to produce a delay of the correct length in C without using a delay function, you do indeed use a for() loop. Hopefully you are using AVR studio, this includes a built in code analyser and step by step feature. Using a while loop instead I believe that While(int i < 54){i++} will yield a delay of about 1 milisecond, I could be wrong though, I say a lot of wrong stuff (THIS IS FOR A 1MHZ SYSTEM). If you are using a for() loop, fill it with ";"'s for nop's, and test on the step by step feature.

    I hope this helps,
     
    Last edited: Dec 4, 2015
  3. 8051Help

    8051Help

    2
    0
    Nov 27, 2015
    Hi, I solved. But now I have problem with 1 second per number.
    I got printing all numbers like results on display. So I need 1second interrupt per number like results
    COde below is little bit different..But working... Only problem is 1 second per number

    Code:
    #include <reg51.h>
    #include <math.h>
    typedef unsigned char byte;
    
    byte a[8]= {7,5,16,3,1,4,11,9};
    
    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=0x3C;
    TL0=0xAF;
    TR0=1;
    counter=1;
    }
    
    
    void timer0(void) interrupt 1 using 2
    {
    TH0=0x3C;
    TL0=0xAF;
    
    counter--;
    if(counter == 0)
    {
    counter = 20;
    if(started == 1)
    {
    if(i < 8)
    if(!(a[i]%3) )
    display=a[i];
    i++;
    if(i == 8)
    {
    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=0x3C;
    TL0=0xAF;
    ET0=1;
    TR0=1;
    }
    }
    }
    
    
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-