当前位置:网站首页>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

边栏推荐
- Is it safe for CITIC Securities Commission to open an online account and speculate in stocks
- Ten thousand words explanation - mindarmour Xiaobai tutorial!
- Simple and fast digital network (network dolls in the network)
- 高清滑环生产过程当中的质量如何把控
- 3 - wire SPI Screen Drive
- 小白看MySQL--windows环境安装MySQL
- 气液滑环与其他滑环的工作原理有什么区别
- MATLAB data type - character type
- How to control the quality of HD slip ring in the production process
- 世界很大,有人把二维码纹在脖子上
猜你喜欢

光谱共焦如何测量玻璃基板厚度

Other service registration and discovery

What is the difference between the working principle of gas-liquid slip ring and other slip rings

温故知新--常温常新

基于SSMP的宠物医院管理系统

滑环选型选购时需要注意的技巧

ArcGIS 镶嵌数据集切片丢失问题处理

當Transformer遇見偏微分方程求解

05 | 规范设计(下):commit 信息风格迥异、难以阅读,如何规范?

Is there anyone who doesn't know the three cores of concurrent programming?
随机推荐
About Random Numbers
简单快速的数网络(网络中的网络套娃)
“message“:“Bad capabilities. Specify either app or appTopLevelWindow to create a session“
find_ Detailed use guide of CIRC
CPU的异常处理
【Mysql】时间字段默认设置为当前时间
XML learning notes
matlab数据类型 —— 字符型
Oracle database basics concepts
Flink 实战问题(七):No Watermark(Watermarks are only available EventTime is used)
Pet hospital management system based on SSMP
Lambda表达式
直播回顾 | 子芽&CCF TF:云原生场景下软件供应链风险治理技术浅谈
Hit the point! The largest model training collection!
Statistical Hypothesis Testing
Oracle 数据库基本知识概念
Implementation of ARP module in LwIP
Moher College - SQL injection vulnerability test (error reporting and blind note)
Simulation of delta variant strain of novel coronavirus (mindsponge application)
墨者学院-SQL注入漏洞测试(报错盲注)