当前位置:网站首页>Solve the problem that stc8g1k08 program cannot run and port configuration

Solve the problem that stc8g1k08 program cannot run and port configuration

2022-06-27 00:53:00 arenascat

Some time ago I got some samples , Then go to the board test

Automatic operation after power on IO Alternate output program , That is, the factory procedure . This proves that there is no problem with playing the board , It can be used normally. .

But here's the problem , I find my own program doesn't work . For example, such a simple lighting program .


#include "reg51.h"
#include "intrins.h"
sbit led1 = P1^1;
sbit led2 = P1^2;
sbit led3 = P1^3;
sbit led4 = P1^4;



void delay(int tim)		//@11.0592MHz
{
	unsigned char i, j;

	i = 20;
	j = 109;
	for(i=0;i<tim;i++)
	{
		do
	{
		while (--j);
	} while (--i);
	}
	
}


void main()
{
	while(1)
	{
		led2 = 1;
		delay(200);
		led2 = 0;
		delay(200);
	}
}

Then find out why , First, let's look and say 3.0 3.1 3.2 At least one pin must be pulled up , Then I will pull up , I got one 10k Resistance in VCC and P3.0 Between .

The result is , Not yet. , What kind of situation is this ?

I read the manual later , Find such a passage ,IO In this generation (STC-Y6) Must be manually configured in , Can not continue to use the previous C51 SCM development thinking , Direct assignment .

So how to configure input and output , This is in STC-ISP The example program on the software is in the window , Just copy it

So the code is modified as follows , The port mode setting has been added


#include "reg51.h"
#include "intrins.h"
sbit led1 = P1^1;
sbit led2 = P1^2;
sbit led3 = P1^3;
sbit led4 = P1^4;

sfr     P0M0        =   0x94;
sfr     P0M1        =   0x93;
sfr     P1M0        =   0x92;
sfr     P1M1        =   0x91;
sfr     P2M0        =   0x96;
sfr     P2M1        =   0x95;
sfr     P3M0        =   0xb2;
sfr     P3M1        =   0xb1;
sfr     P4M0        =   0xb4;
sfr     P4M1        =   0xb3;
sfr     P5M0        =   0xca;
sfr     P5M1        =   0xc9;
sfr     P6M0        =   0xcc;
sfr     P6M1        =   0xcb;
sfr     P7M0        =   0xe2;
sfr     P7M1        =   0xe1;

void delay(int tim)		//@11.0592MHz
{
	unsigned char i, j;

	i = 20;
	j = 109;
	for(i=0;i<tim;i++)
	{
		do
	{
		while (--j);
	} while (--i);
	}
	
}


void main()
{
	P1M0 = 0x00;                                //Set P1 OUTPUT
  P1M1 = 0x00;
	while(1)
	{
		led1 = 1;
		delay(200);
		led1 = 0;
		delay(200);
	}
}

The official tutorial also has a similar , For example, configure the two-way port read / write operation .

#include "reg51.h"
#include "intrins.h"

sfr     P0M0        =   0x94;
sfr     P0M1        =   0x93;
sbit    P00         =   P0^0;

void main()
{
    P0M0 = 0x00;                                // Set up P0.0~P0.7 It is a two-way port mode 
    P0M1 = 0x00;

    P00 = 1;                                    //P0.0 Port output high level 
    P00 = 0;                                    //P0.0 Port output low level 

    P00 = 1;                                    // Enable the internal weak pull-up resistor before reading the port 
    _nop_();                                    // Wait two clocks 
    _nop_();                                    //
    CY = P00;                                   // Read port status 

    while (1);
}

But still not , At this time, I went to look for someone else's code , Like this one https://oshwhub.com/Fangbrbr/lu-you-zhuai-jie

  I read it for a while before I knew , indicating surprise or wonder , The original header file has now become a new one , It was time to develop , Is to use #include "reg51.h" Now this sentence has become #include "STC8.h" , And I didn't use it, so I couldn't let LED Light up .

#include "intrins.h"
#include "STC8.h"


void Delay300ms()		//@24.000MHz
{
	unsigned char i, j, k;

	_nop_();
	_nop_();
	i = 37;
	j = 135;
	k = 138;
	do
	{
		do
		{
			while (--k);
		} while (--j);
	} while (--i);
}

void main()
{
	P1M0=0x00;
	P1M1=0x00;
  while(1)
	{
		P11=0;
		Delay300ms();
		P11=1;
		Delay300ms();
   }
}

Modify your own code , The main thing is to change the header file , Because the new header file has basically defined some definitions , So many do not need to be in main Then write in the document


#include "STC8.h"
#include "intrins.h"
sbit led1 = P1^1;
sbit led2 = P1^2;
sbit led3 = P1^3;
sbit led4 = P1^4;


void delay(int tim)		//@11.0592MHz
{
	unsigned char i, j;
  int k;
	i = 15;
	j = 90;
	for(k=0;k<tim;k++)
	{
			do
		{
			while (--j);
		} while (--i);
	}
	
}


void main()
{
	P1M0 = 0x00;                                //Set P1 OUTPUT
  P1M1 = 0x00;
	P1 = 0x00;
	while(1)
	{
    delay(10);
		P1 = P1<<1;
		delay(10);
		if(P1==0)
		{
		P1 = 0x01;
		}
		if(P1>0x0F)
		{
			P1 = 0x00;
		}
	}
}

 OK, Now the test is successful

Port configuration

be-all I/O Every mouth has 4 Working mode : Quasi two-way port / Weak pull-up ( standard 8051 Output port mode )、 Push pull output / Strong pull up 、 high Resistive input ( Current can neither flow in nor out )、 Open drain output . Software can be used to I/O The working mode of the port is easy to configure

except P3.0 and P3.1 Outside , All the rest I/O After the port is powered on, the state is high resistance input state , The user is making use I/O The port must be set first I/O Mouth mode

   P0M0 = 0x00;                                // Set up P0.0~P0.7 It is a two-way port mode 
    P0M1 = 0x00;
    P1M0 = 0xff;                                // Set up P1.0~P1.7 For push-pull output mode 
    P1M1 = 0x00;
    P2M0 = 0x00;                                // Set up P2.0~P2.7 High impedance input mode 
    P2M1 = 0xff;
    P3M0 = 0xff;                                // Set up P3.0~P3.7 Open drain mode 
    P3M1 = 0xff;

How to set up , For example, all the P1 and P3 The interface is set to bidirectional mode , The current is weak at this time

P1M0 = 0x00;                                //Set P1 OUTPUT
P1M1 = 0x00;
P3M0 = 0x00;                                //Set P3 OUTPUT
P3M1 = 0x00;

Then I need to use P37 As push-pull output , At this time, you can use this sentence ( The header file is required to contain STC8.h)

P3PU = 0x40;

Besides the first time, there is Schmidt trigger control register PxNCS, Level rate conversion PxSR, Current control register PxDR, Input enable register PxIE

(x=0,1,2,3,4,5....)

STC8GK01 Electrical characteristics of

 

原网站

版权声明
本文为[arenascat]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/178/202206270009550309.html