EngineersGarage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise
You are here: Home / Topics / Counter up/down

Counter up/down

|

Microcontroller › 8051 › Counter up/down

  • This topic has 0 replies, 1 voice, and was last updated 12 years, 8 months ago by V.
Viewing 1 post (of 1 total)
  • Author
    Posts
  • August 6, 2013 at 8:31 pm #2577
    V
    Participant

    Hello, I have a problem with this program. When I call void izobrazqvane () my program breakdown.

     

    #include <at89x51.h>
    #include “delay.h”
    #include “lcd.h”
    #define PRELOAD01 (65536 – (tWord)(OSC_FREQ / (OSC_PER_INST * 1020)))
    #define PRELOAD01H (PRELOAD01 / 256)
    #define PRELOAD01L (PRELOAD01 % 256)

    #define LED1 P1_6 //sin
    #define LED2 P1_7 //cherven
    #define BUT1 P3_4
    #define BUT2 P3_5
    #define CNTZAK
    #define LCD_RS P1_5
    #define LCD_EN P1_4
    #define LCD_DATA P1

    __data __at (0x020) unsigned char BUT1FL;

    __bit __at (0x00) BUT1FL_0;
    __bit __at (0x01) BUT1FL_1;
    __bit __at (0x02) BUT1FL_2;
    __bit __at (0x03) BUT1FL_3;
    __bit __at (0x04) BUT1FL_4;
    __bit __at (0x05) BUT1FL_5;
    __bit __at (0x06) BUT1FL_6;
    __bit __at (0x07) BUT1FL_7;

    __data __at (0x022) unsigned char BUT2FL;

    __bit __at (0x10) BUT2FL_0;
    __bit __at (0x11) BUT2FL_1;
    __bit __at (0x12) BUT2FL_2;
    __bit __at (0x13) BUT2FL_3;
    __bit __at (0x14) BUT2FL_4;
    __bit __at (0x15) BUT2FL_5;
    __bit __at (0x16) BUT2FL_6;
    __bit __at (0x17) BUT2FL_7;

    __data __at (0x030) unsigned char CNT1;
    __data __at (0x031) unsigned char CNTB1;
    __data __at (0x032) unsigned char CNT2;
    __data __at (0x033) unsigned char CNTB2;
    /*__data __at (0x034) unsigned char CNT3;
    __data __at (0x035) unsigned char CNT4;
    __data __at (0x036) unsigned char CNT1;*/
    void izobrazqvane () //
    {
    unsigned char counter_button[1];
    counter_button[0] = CNT2+0x30;
    lcd_puts(counter_button); //display on LCD
    }

    void led_sw (void)
    {

    LED2=!LED2;
    }
    void but1do () // count down
    {

    LED1=!LED1;
    BUT1FL_5=1;
    CNT2=CNT2-5;
    if(CNT2==5)
    {
    CNT2=15;

    }
     izobrazqvane ();// crashes
    }

    void but2do () // count up
    {

    LED1=!LED1;
    BUT2FL_5=1;
    CNT2=CNT2+5;
    if(CNT2==250)
    {
    CNT2=240;

    }
    izobrazqvane ();

    }

    void but1obr ()
    {
    if(BUT1FL_7==1)
    {
    if(BUT1FL_5==0) //ako e 1 – butonat e otraboten
    {
    if(BUT1FL_0==0) //startira 30 ms zakasnenie
    {
    BUT1FL_0=1;
    CNTB1=3;
    }

    else
    {
    if(BUT1FL_2==0)
    {
    CNTB1–;
    if(CNTB1==0)
    {
    BUT1FL_2=1;
    }

    else
    {
    ;
    }
    }

    else
    {
    but1do();
    }
    }
    }

    else
    {
    ;
    }
    }
    else
    {
    BUT1FL=0;
    }
    }

    void but2obr ()
    {
    if(BUT2FL_7==1)
    {
    if(BUT2FL_5==0) //ako e 1 – butonat e otraboten
    {
    if(BUT2FL_0==0) //startira 30 ms zakasnenie
    {
    BUT2FL_0=1;
    CNTB2=3;
    }

    else
    {
    if(BUT2FL_2==0)
    {
    CNTB2–;
    if(CNTB2==0)
    {
    BUT2FL_2=1;
    }

    else
    {
    ;
    }
    }

    else
    {
    but2do();
    }
    }
    }

    else
    {
    ;
    }
    }
    else
    {
    BUT2FL=0;
    }
    }

    void main(void)
    {

    lcd_init();
    // unsigned char counter_button[5];
    // counter_button[0] = CNT2 + 0x30;
    // lcd_goto(3);
    // lcd_puts(“coun”);

    {

    TMOD = 0x11; // timer 0
    TR0 = 1;
    TH0 = 0xDF; //5 ms ?
    TL0 = 0x75;
    ET0 = 1;
    IT0 = 1;
    EA = 1;
    CNT1 = 50; // umnojava timera i se poluchava zakusnenieto
    __asm // nulira operativnata pamet
    MOV R0,#0x2F
    RES1: MOV @R0,#0
    DJNZ R0,RES1
    __endasm;

    SP=0x40;
    }

    while(1)
    {

    if(BUT1==0)
    {
    BUT1FL_7=1;
    }
    else
    {
    BUT1FL_7=0;
    }

    if(BUT2==0)
    {
    BUT2FL_7=1;
    }
    else
    {
    BUT2FL_7=0;
    }

    }
    }

    void timer_0 (void) __interrupt 1
    {
    TH0 = 0xEF; //5 ms
    TL0 = 0xBA;
    CNT1–;
    if(CNT1==0)
    {
    CNT1=CNT2;
    led_sw();
    }

    but1obr();
    but2obr();
    // izobrazqvane ();
    }

    /*============================================================================================
    Programa za LCD
    ==============================================================================================*/

    void LCD_STROBE (void);

    int reverse(unsigned char byte_to_shift)
    {
    unsigned char U8byte_new = byte_to_shift;

    unsigned char bit0_Mask = 0x01;
    unsigned char bit1_Mask = 0x02;
    unsigned char bit2_Mask = 0x04;
    unsigned char bit3_Mask = 0x08;
    unsigned char bit4_Mask = 0x10;
    unsigned char bit5_Mask = 0x20;
    unsigned char bit6_Mask = 0x40;
    unsigned char bit7_Mask = 0x80;

    U8byte_new = ((byte_to_shift&bit7_Mask)>>3)|((byte_to_shift&bit6_Mask)>>1)|((byte_to_shift&bit5_Mask)<<1)|((byte_to_shift&bit4_Mask)<<3)| ((byte_to_shift&bit3_Mask)>>3)|((byte_to_shift&bit2_Mask)>>1)|((byte_to_shift&bit1_Mask)<<1)|((byte_to_shift&bit0_Mask)<<3);

    return U8byte_new;
    }

    void lcd_write(unsigned char c)
    {
    unsigned char data = (unsigned char)(( c >> 4 ) | (LCD_RS << 5)) ;// 0x0F original ||

    // delay_70u();

    LCD_DATA = data;
    LCD_STROBE();
    data = (unsigned char)((c & 0x0F) | (LCD_RS << 5));
    LCD_DATA = data;
    LCD_STROBE();

    }

    /*
    * Clear and home the LCD
    */

    void lcd_clear(void)
    {
    LCD_RS = 0;
    lcd_write(0x1);
    delay_10u();
    }

    /* write a string of chars to the LCD */

    void lcd_puts(const char *s)
    {
    LCD_RS = 1; // write characters
    while(*s)
    {
    lcd_write(reverse(*s));
    s++;
    }
    }

    /* write one character to the LCD */

    void lcd_putch(char c)
    {
    LCD_RS = 1; // write characters
    lcd_write( c );
    }

    /*
    * Go to the specified position
    */

    void lcd_goto(unsigned char pos)
    {
    LCD_RS = 0;
    lcd_write(0x80+pos);
    }

    /* initialise the LCD – put into 4 bit mode */

    void lcd_init()
    {
    char init_value;

    init_value = 0x3;

    LCD_RS = 0;
    LCD_EN = 0;

    delay_70ms(); // delay for 150ms
    delay_70ms();
    delay_10ms();

    LCD_DATA =0xC;
    LCD_STROBE();
    delay_5ms();

    LCD_DATA =0xC;
    LCD_STROBE();
    delay_100u();

    LCD_DATA =0xC;
    LCD_STROBE();
    delay_100u();

    LCD_DATA =0x4;
    LCD_STROBE();
    delay_100u();

    LCD_DATA =0x4;
    LCD_STROBE();
    LCD_DATA =0x1;
    LCD_STROBE();
    delay_100u();

    LCD_DATA =0x0;
    LCD_STROBE();
    LCD_DATA =0x1;
    LCD_STROBE();
    delay_100u();

    LCD_DATA =0x0;
    LCD_STROBE();
    LCD_DATA =0x8;
    LCD_STROBE();
    delay_5ms();

    LCD_DATA =0x0;
    LCD_STROBE();
    LCD_DATA =0x6;
    LCD_STROBE();
    delay_100u();

    LCD_DATA =0x0;
    LCD_STROBE();
    LCD_DATA =0x3;
    LCD_STROBE();
    delay_100u();

    LCD_RS = 1;
    }

    void LCD_STROBE(void)
    {
    LCD_EN = 1;
    delay_10u();
    LCD_EN= 0;
    }

     

  • Author
    Posts
Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.
Log In

RSS Recent Posts

  • Getting into an LED bulb April 21, 2026
  • understanding of resonance in time domain April 21, 2026
  • Beginner Questions About CNC Machines – G-code, Control Systems & Accuracy April 21, 2026
  • A Must-Watch Video Showing Dangerous Construction of Cheap Lithium-Ion Cells April 21, 2026
  • S1MJ ? April 20, 2026

Stay Up To Date

Newsletter Signup
EngineersGarage

Copyright © 2026 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise