端口模式配置
单片机上电后,所有I/O口(P3.0和P3.1除外)均为高阻输入模式,端口使用前必须配置I/O口模式。
PnM1.x | PnM0.x | Pn.x口工作模式 |
---|---|---|
0 | 0 | 准双向口 |
0 | 1 | 推挽输出 |
1 | 0 | 高阻输入 |
1 | 1 | 开漏模式 |
main.c
#include <STC8H.H>
#include "delay.h"
sbit LED = P2^0; //灌电流,低电平点亮
void main(void)
{
/*P2.0 准双向口模式*/
P2M0 = 0x00;
P2M1 = 0xfe;
while(1)
{
LED = 0; //亮
delay_ms(250);
LED = 1; //灭
delay_ms(250);
}
}
delay.h
#ifndef __DELAY_H
#define __DELAY_H
//#define MAIN_Fosc 22118400L //定义主时钟
//#define MAIN_Fosc 12000000L //定义主时钟
//#define MAIN_Fosc 11059200L //定义主时钟
//#define MAIN_Fosc 5529600L //定义主时钟
#define MAIN_Fosc 24000000L //定义主时钟
void delay_ms(unsigned char ms);
#endif
delay.c
/*---------------------------------------------------------------------*/
/* --- STC MCU Limited ------------------------------------------------*/
/* --- STC 1T Series MCU Demo Programme -------------------------------*/
/* --- Mobile: (86)13922805190 ----------------------------------------*/
/* --- Fax: 86-0513-55012956,55012947,55012969 ------------------------*/
/* --- Tel: 86-0513-55012928,55012929,55012966 ------------------------*/
/* --- Web: www.STCMCU.com --------------------------------------------*/
/* --- Web: www.STCMCUDATA.com ---------------------------------------*/
/* --- QQ: 800003751 -------------------------------------------------*/
/* 如果要在程序中使用此代码,请在程序中注明使用了STC的资料及程序 */
/*---------------------------------------------------------------------*/
/*************** 功能说明 ****************
本文件为STC8系列的延时程序,用户几乎可以不修改这个程序.
******************************************/
//========================================================================
// 函数: void delay_ms(unsigned char ms)
// 描述: 延时函数。
// 参数: ms,要延时的ms数, 这里只支持1~255ms. 自动适应主时钟.
// 返回: none.
// 版本: VER1.0
// 日期: 2021-3-9
// 备注:
//========================================================================
#include "delay.h"
void delay_ms(unsigned char ms)
{
unsigned int i;
do{
i = MAIN_Fosc/10000;
while(--i);
}while(--ms);
}