STC15F104W encoder two-way adjustable PWM output source program

Publisher:知识的海洋Latest update time:2020-12-09 Source: 51heiKeywords:STC15F104W Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere


//I didn't use the IAPIDLE function. Is there any expert who can give me some advice on the pros and cons?

#include <STC15F104E_PLUS.h>

#include

typedef unsigned char BYTE;

typedef unsigned int WORD;

//#define uint unsigned int

//#define MAIN_Fosc 22118400L //Define the main clock

//#define MAIN_Fosc 12000000L //Define the main clock

#define MAIN_Fosc 11059200L //Define the main clock

//#define MAIN_Fosc 5529600L //Define the main clock

//#define MAIN_Fosc 24000000L //Define the main clock

//#define MAIN_Fosc 40000000L //Define the main clock

//#define ENABLE_IAP 0x80           //if SYSCLK<30MHz

//#define ENABLE_IAP 0x81           //if SYSCLK<24MHz

//#define ENABLE_IAP  0x82          //if SYSCLK<20MHz

#define ENABLE_IAP 0x83           //if SYSCLK<12MHz

//#define ENABLE_IAP 0x84           //if SYSCLK<6MHz

//#define ENABLE_IAP 0x85           //if SYSCLK<3MHz

//#define ENABLE_IAP 0x86           //if SYSCLK<2MHz

//#define ENABLE_IAP 0x87           //if SYSCLK<1MHz

#define CMD_IDLE 0 //Idle mode

#define CMD_READ 1 //IAP byte read command

#define CMD_PROGRAM 2 //IAP byte programming command

#define CMD_ERASE 3 //IAP sector erase command

//Test address

#define IAP_ADDRESS1 0X0000 //Internal EEPROM address

#define IAP_ADDRESS2 0X0200

/* Initial definition */

bit  write = 0;

sbit VD=P3^3; //Red LED 0.50v=10v ;1.00=27v ;3.85=73v;

sbit CD=P3^4; //Green LED

sbit ZS=P3^5; //Indicates LED switching 0 - Red LED adjustment; 1 - Green LED adjustment

sbit BA=P3^0; //Connect encoder pin a to P3.0

sbit BB=P3^1; //Connect encoder's pin b to P3.1

sbit QH=P3^2; //Encoder button connected to P3.2 voltage adjustment or current adjustment 299=5.00V

BYTE v_temp,c_temp,XZ = 0,flag,a0,b0,c0;//  128 109  48 29  //48=0.96V;29=0.58V;

void InitTimer0() interrupt 1 // 0.5 milliseconds @ 11.0592MHz frequency 1000HZ

{

    IE2 = 0x00; //Disable timer 2 interrupt

    AUXR = 0XE4; // turn off timer 2 counting

    TL0 = 0xCD; //Set the initial timing value to 0.5 milliseconds @ 11.0592MHz

    TH0 = 0xD4; //Set the initial timing value to 0.5 milliseconds @ 11.0592MHz

    CEO = 1;

    CD = 1;

    flag = 1;

    AUXR = 0xF4; //Start timer 2 counting

    IE2 = 0x04; // Enable timer 2 interrupt

}


void InitTimer2() interrupt 12 //2 microseconds @ 11.0592MHz to control the light on time

{

    flag++;

    T2L = 0xD4; //Set the initial timing value to 2 microseconds @ 11.0592MHz

    T2H = 0xFF; //Set the initial timing value to 2 microseconds @ 11.0592MHz

    if(v_temp == flag) VD=0;

    if(c_temp == flag) CD=0;

}


/*//Disable IAP

void IapIdle()

{

    IAP_CONTR = 0; //Disable IAP function

    IAP_CMD = 0; // Clear command register

    IAP_TRIG = 0; // Clear the trigger register

    IAP_ADDRH = 0x80; //Set the address to the non-IAP area

    IAP_ADDRL = 0;

}

*/

//Read a byte from the ISP/IAP/EEPROM area

BYTE IapReadByte(WORD addr)

{

    BYTE dat; //data buffer

    IAP_CONTR = ENABLE_IAP;         //使能IAP

    IAP_CMD = CMD_READ; //Set IAP command

    IAP_ADDRL = addr; //Set IAP low address

    IAP_ADDRH = addr >> 8; //Set IAP high address

    IAP_TRIG = 0x5a; //Write trigger command (0x5a)

    IAP_TRIG = 0xa5; //Write trigger command (0xa5)

    _nop_(); //Wait for ISP/IAP/EEPROM operation to complete

    dat = IAP_DATA; //Read ISP/IAP/EEPROM data

    //IapIdle(); //Disable IAP function

    return dat; //return


}


//Write one byte of data to the ISP/IAP/EEPROM area

void IapProgramByte(WORD addr, BYTE dat)

{

    IAP_CONTR = ENABLE_IAP;         //使能IAP

    IAP_CMD = CMD_PROGRAM; //Set IAP command

    IAP_ADDRL = addr; //Set IAP low address

    IAP_ADDRH = addr >> 8; //Set IAP high address

    IAP_DATA = dat; //Write ISP/IAP/EEPROM data

    IAP_TRIG = 0x5a; //Write trigger command (0x5a)

    IAP_TRIG = 0xa5; //Write trigger command (0xa5)

    _nop_(); //Wait for ISP/IAP/EEPROM operation to complete

    //IdleIdle();


}


// sector erase

void IapEraseSector(WORD addr)

{

    IAP_CONTR = ENABLE_IAP;         //使能IAP

    IAP_CMD = CMD_ERASE; //Set IAP command

    IAP_ADDRL = addr; //Set IAP low address

    IAP_ADDRH = addr >> 8; //Set IAP high address

    IAP_TRIG = 0x5a; //Write trigger command (0x5a)

    IAP_TRIG = 0xa5; //Write trigger command (0xa5)

    _nop_(); //Wait for ISP/IAP/EEPROM operation to complete

    //IdleIdle();


}



/*void Timer0Init(void) //100 microseconds @ 24.000MHz 10KHZPWM

{

        AUXR |= 0x80; //Timer clock 1T mode

        TMOD &= 0xF0; //Set timer mode

        TL0 = 0xA0; //Set the initial timing value--100 microseconds @ 24.000MHz

        TH0 = 0xF6; //Set the initial timing value--100 microseconds @ 24.000MHz

        TF0 = 0; // Clear TF0 flag

        TR0 = 1; //Timer 0 starts timing

}


void Timer2Init(void) //0.4 microseconds @ 24.000MHz

{

        AUXR |= 0x04; //Timer clock 1T mode

        T2L = 0xF6; //Set the initial timing value--0.4 microseconds @ 24.000MHz

        T2H = 0xFF; //Set the initial timing value--0.4 microseconds @ 24.000MHz

        AUXR |= 0x10; //Timer 2 starts timing

}

*/

void bianmaqi(void) //Capture EC11 press and rotation information

{

  unsigned char a, b, c;

  a = BA; b = BB; c = QH;

   if (a != a0)     // BA changed

  {

        a0 = a;

    if (b != b0)    // BB changed

     {

      b0 = b;write = 1;

        if ((a == b) && (XZ == 0) && (v_temp < 153)) {v_temp++;}//225 153

    if ((a == b) && (XZ == 1) && (c_temp < 73)) {c_temp++;}//208 65

    if ((a != b) && (XZ == 0) && (v_temp > 23)) {v_temp--;}//85 23   

        if ((a != b) && (XZ == 1) && (c_temp > 5)) {c_temp--;}//29 5

     }

    }

  if (!c && c0)

        {

        if (XZ == 0 )

      {

        XZ = 1;

        ZS = 1;

        }

    else if(XZ == 1)//switch

      {

            XZ = 0;

        ZS = 0;

       }

    }

    c0 = c;

}

void init()

{

    AUXR = 0XE4; //Set timer 0 and 2 to 1T mode

    TMOD = 0x00;

    TL0 = 0xCD; //Set the initial timing value to 0.5 milliseconds @ 11.0592MHz 0xA0; -- 100 microseconds @ 24.000MHz

    TH0 = 0xD4; //Set the initial timing value to 0.5 milliseconds @ 11.0592MHz 0xFF; -- 0.4 microseconds @ 24.000MHz

    T2L = 0xD4; //Set the initial timing value to 2 microseconds @ 11.0592MHz 0xF6;--0.4 microseconds @ 24.000MHz

    T2H = 0xFF; //Set the initial timing value to 2 microseconds @ 11.0592MHz 0xFF;--0.4 microseconds @ 24.000MHz

    EA = 1;

    ET0 = 1;

    TR0 = 1; //Timer 0 starts timing

    P3M1 &= 0xF7;

    P3M0 |= 0x08; //P3.3 (push-pull)

    P3M1 &= 0xEF;

    P3M0 |= 0x10; //P3.4 (push-pull)

    P3M1 &= 0xDF;

    P3M0 |= 0x20; //P3.5 (push-pull)

    CEO = 0;

    CD = 0;

    ZS = 0; // 0 - voltage regulation state; 1 - current regulation state

    a0 = NO;

    b0 = BB;

    c0 = QH;

          v_temp = IapReadByte(1);

        if (v_temp == 0 ){v_temp = 65;}     //128        65

        if (v_temp > 153){v_temp = 65;}     //245  128   

        c_temp = IapReadByte(10);

    if (c_temp == 0 ){c_temp = 29;}     //109           

    if (c_temp > 29 ){c_temp = 29;}     //208  109  


}

void main()

{

heat();

while(1)

   {

   bianmaqi();

   if (write == 1) {

          write = 0;TR0 = 0;

      IapEraseSector(IAP_ADDRESS1); //Erase sector

      if (XZ == 0 )  {

      IapProgramByte(1,v_temp); //Write data

                   }

        else  if (XZ == 1 )  {

      IapProgramByte(10,c_temp); //Write data

                   }

   }

   _nop_();

   TR0 = 1;

   }

}


Keywords:STC15F104W Reference address:STC15F104W encoder two-way adjustable PWM output source program

Previous article:DS18B20 single chip digital thermometer
Next article:Body temperature monitoring sound and light alarm device based on DS18B20

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号