stc89c52在Linux或MacOS下使用sdcc和stcgal中断与LCD测试代码

2020-10-09 10:46:31 浏览数 (1)

使用说明参考:https://cloud.tencent.com/developer/article/1796577

外部中断:

代码语言:javascript复制
#include<8052.h>

#define LSA P1_5					
#define LSB P1_6
#define LSC P1_7

void EXint_Init(void);
void Delayms(unsigned int);
void Down2Up(int);		        
void Up2Down(int);				
void EXINT0() __interrupt 0;
void EXINT1() __interrupt 2;

void main()
{
	EXint_Init();	
	LSA=0;
	LSB=0;
	LSC=0;						
	while(1)					
	{
		P0 = 0xff;			
		Delayms(250);	         
		P0 = 0x00;				
		Delayms(250);
	}                         
}

void EXint_Init()              
{
	IT0=1;                     
	IT1=0;                     
//	IPH=0x40;                   
	PX1=1;                     
	EA=1;                       
	EX0=1;                      
	EX1=1;                      
}


void Delayms(unsigned int xms)
{
	unsigned int i,j;
	for(i = xms;i > 0;i--)
	{
		for (j = 110;j > 0;j--);
	}
}

void EXINT0() __interrupt 0     	
{
	Down2Up(3);					
}

void EXINT1() __interrupt 2       
{
	Up2Down(3);					
}

void Down2Up(int x) 				 
{	
	int i, j;
	unsigned char sel=0xfe;						
	for(i=0;i<x;i  )			
	{	
		for(j=0;j<8;j  )		
		{	
			P0 = sel;
			Delayms(250);		
			sel =sel<<1; 	
		}
		sel=0xfe;						
	}
}

void Up2Down(int x) 		
{	
	int i, j;
	unsigned char sel=0x7f;								
	for(i=0;i<x;i  )			
	{	
		for(j=0;j<8;j  )		
		{	
			P0 = sel;
			Delayms(250);	
			sel=sel>>1; 	
		}
		sel=0x7f;						
	}
}

定时器中断:

代码语言:javascript复制
#include <8052.h>
#define  Wave   P0_1		        //位定义,定义完之后,以下 Wave 即代表P0.1
#define 	LSA    P1_5	   		// P0口所接的发光二极管由开发板上的WaveS0控制, 
#define 	LSB    P1_6	   		//且该位连接到74HC138三八译码器的输出端,
#define 	LSC    P1_7	   		//因此P1.5,P1.6,P1.7需赋值0以选中该位。
unsigned char cnt = 0;                  //定义一个计数变量,记录 T0 溢出次数
void Timer0() __interrupt 1;

/************************************************************/
void main ()					           //主函数
{
	LSA=0;						   //本开发板的P0发光二极管片选地址
	LSB=0;
	LSC=0;
	Wave = 0;					   //初始化P1.0=0
	TMOD = 0x01;				           //设置定时器T0工作方式1
	TL0  = 47104%6;			           //计数器初值
	TH0  = 47104/256;
	TR0  = 1;					   //启动定时器
	ET0  = 1;					   //开中断
	EA   = 1;
	while(1);                                          //模拟主程序其它工作
}
 /************************************************************/
void Timer0() __interrupt 1		        //定时器 T0 中断响应
{
 	TL0  = 47104%6;			//计数器初值
	TH0  = 47104/256;
	cnt  ; //计数值自加 1
 	if (cnt > 49)                           //判断 T0 溢出是否达到 50 次,100次
 	{
	 Wave=1;
	 if (cnt > 99)
	 {
		 cnt=0;
		 Wave=0;
	 }
	 //cnt = 0; //达到 50 次后计数值清零
	 /*if( Wave = 0 )
	 {
		 Wave = 1;
	 }
	 else
	 {
		 Wave = 0;
	 }*/
	 
	 //Wave = ~Wave; //Wave 取反:0-->1、1-->0
 	}
}

helloworld:

代码语言:javascript复制
#include<8052.h>
 
#define uchar unsigned char
#define uint unsigned int
 
#define lcden P1_2
#define lcdrs P1_0
#define rw P1_1
 
uchar table[] = "Hello World!";
uchar num;
 
void delay(uint z) {
    uint x, y;
    for(x = z; x > 0; x--)
        for(y = 110; y > 0; y--);
}
 
void write_com(uchar com) {
    lcdrs = 0;
    P0 = com;
    delay(5);
    lcden = 1;
    delay(5);
    lcden = 0;
}
 
void write_data(uchar date) {
    lcdrs = 1;
    P0 = date;
    delay(5);
    lcden = 1;
    delay(5);
    lcden = 0;
}
 
void init() {	
    rw = 0;
    lcden = 0;
    write_com(0x38);
    write_com(0x0e);
    write_com(0x06);
    write_com(0x01);
    write_com(0x80);
}
 
void main() {
    init();
    for(num = 0; num < 12; num  ) {
        write_data(table[num]);
        delay(100);
    }
    while(1);
}

0 人点赞