当前位置:网站首页>解决STC8G1K08程序不能运行的问题和端口配置
解决STC8G1K08程序不能运行的问题和端口配置
2022-06-27 00:10:00 【arenascat】
前一段时间我拿到一些样片,然后去打板测试

上电后自动运行IO轮流输出的程序,也就是出厂程序。这证明打的板子没有问题,可以正常使用。

但是问题就来了,我发现我自己的程序跑不起来。比如这么一个最简单的点灯程序。
#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);
}
}之后就找原因,先是看说3.0 3.1 3.2 必须至少有一个引脚是上拉,那我就上拉,接了一个10k电阻在VCC和P3.0之间。
结果就是,还是不行,那这个算是什么情况呢?
后面我看了一下手册,发现这样一段话,IO口在这一代(STC-Y6)中是必须要手动进行配置的,不能沿用以前C51单片机的开发思维,直接赋值。

那么如何配置输入输出呢,这个在STC-ISP软件上的范例程序这一窗口里就有了,直接抄一下

所以代码修改为这样,大致就是增加了点儿端口模式设置
#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);
}
}官方的教程中也有类似的,比如这样的配置双向口读写操作。
#include "reg51.h"
#include "intrins.h"
sfr P0M0 = 0x94;
sfr P0M1 = 0x93;
sbit P00 = P0^0;
void main()
{
P0M0 = 0x00; //设置P0.0~P0.7为双向口模式
P0M1 = 0x00;
P00 = 1; //P0.0口输出高电平
P00 = 0; //P0.0口输出低电平
P00 = 1; //读取端口前先使能内部弱上拉电阻
_nop_(); //等待两个时钟
_nop_(); //
CY = P00; //读取端口状态
while (1);
}
但是还是不行,这时候我就去找找别人的代码了,比如这一个 https://oshwhub.com/Fangbrbr/lu-you-zhuai-jie

我看了下才知道,嚯,原来是头文件现在也变成新的了,以前时候开发,都是使用#include "reg51.h" 现在这句话变成了#include "STC8.h" , 而我没有用对所以就没法让LED亮起来。
#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();
}
}
修改一下自己的代码,主要就改了头文件,因为新的头文件基本把一些定义都定好了,所以很多不需要在main文件里再写
#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,现在测试成功

端口的配置
所有的 I/O 口均有 4 种工作模式:准双向口/弱上拉(标准 8051 输出口模式)、推挽输出/强上拉、高 阻输入(电流既不能流入也不能流出)、开漏输出。可使用软件对 I/O 口的工作模式进行容易配置
除 P3.0 和 P3.1 外,其余所有 I/O 口上电后的状态均为高阻输入状态,用户在使 用 I/O 口时必须先设置 I/O 口模式
P0M0 = 0x00; //设置P0.0~P0.7为双向口模式
P0M1 = 0x00;
P1M0 = 0xff; //设置P1.0~P1.7为推挽输出模式
P1M1 = 0x00;
P2M0 = 0x00; //设置P2.0~P2.7为高阻输入模式
P2M1 = 0xff;
P3M0 = 0xff; //设置P3.0~P3.7为开漏模式
P3M1 = 0xff;如何设置,比如说要把所有P1和P3接口设置为双向模式,此时电流比较弱
P1M0 = 0x00; //Set P1 OUTPUT
P1M1 = 0x00;
P3M0 = 0x00; //Set P3 OUTPUT
P3M1 = 0x00;然后我需要用一下P37作为推挽输出,这时候用这一句就可以(需要头文件包含STC8.h)
P3PU = 0x40;初次之外还有施密特触发控制寄存器PxNCS,电平速率转换PxSR,电流控制寄存器PxDR,输入使能寄存器PxIE
(x=0,1,2,3,4,5....)
STC8GK01的电气特性

边栏推荐
- Lwip之ARP模块实现
- 国产框架MindSpore联合山水自然保护中心,寻找、保护「中华水塔」中的宝藏生命
- Pet hospital management system based on SSMP
- xml学习笔记
- BootstrapBlazor + FreeSql实战 Chart 图表使用(2)
- 根据文件名批量生成文件夹
- 目前哪个证券公司炒股开户是最好最安全的?
- Overview of Freescale MCU
- Employment prospect of GIS and remote sensing specialty and ranking selection of universities in 2022
- Employment prospect of GIS and remote sensing specialty and ranking selection of universities in 2022
猜你喜欢

com.fasterxml.jackson.databind.exc.MismatchedInputException: Expected array or string. at [Source:x

CPU exception handling

新型冠状病毒变异Delta毒株的模拟(MindSPONGE应用)

万字详解-MindArmour 小白教程!

Deep learning method for solving mean field game theory problems

Oracle 數據庫基本知識概念

Employment prospect of GIS and remote sensing specialty and ranking selection of universities in 2022

Using physical information neural network to solve hydrodynamics equations

Analysis on the advantages and disadvantages of the best 12 project management systems at home and abroad
![自定义JSP[if,foreach,数据,select]标签](/img/a2/fc75c182d572d86f4466323e31d6c3.png)
自定义JSP[if,foreach,数据,select]标签
随机推荐
基于SSMP的宠物医院管理系统
What are the skills and methods for slip ring installation
国产框架MindSpore联合山水自然保护中心,寻找、保护「中华水塔」中的宝藏生命
Record a bug caused by a line break
复杂数据没头绪?
Using physical information neural network to solve hydrodynamics equations
Other service registration and discovery
My advanced learning notes of C language ----- keywords
高清滑环生产过程当中的质量如何把控
【leetcode】275. H index II
【Mysql】时间字段默认设置为当前时间
Lambda表达式
Mindspire, a domestic framework, cooperates with Shanshui nature conservation center to find and protect the treasure life in the "China water tower"
Amway! How to provide high-quality issue? That's what Xueba wrote!
Introduction to message queuing
网络中的网络(套娃)
如何写好测试用例以及go单元测试工具testify简单介绍
数据库面试题+sql语句解析
The most complete hybrid precision training principle in the whole network
目前哪个证券公司炒股开户是最好最安全的?