⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mysci.c

📁 DSP2407串行总线的简单应用
💻 C
字号:
#include "LF2407REGS.h"
#define uchar unsigned char
#define uint  unsigned int
#define clear   0x01  //清屏幕,约1.6ms 清除显示屏幕,把DDRAM位址计数器调整为"00H"
#define home    0x02  //把DDRAM位址计数器调整为"00H",游标回原点,该功能不影响显示DDRAM
#define f_set   0x30  //基本指令集动作
#define dis_on  0x0f  //打开显示,光标,闪烁功能
#define in_mode 0x06  //输入方式,地址增1,光标右移
 uchar  receive[10];
 uchar jj=0;  //static             
void inline disable() 
{
	asm(" setc INTM ");
}
// 开总中断程序
void inline enable()
{
	asm(" clrc INTM ");
}
/*************************************/
void interrupt nothing()
{
 return;
}

/*************************************/
void DSP_initial(void)
{
    asm(" setc	SXM ");			// 符号位扩展有效
	asm(" clrc	OVM ");			// 累加器中结果正常溢出
	asm(" clrc	CNF ");			// B0被配置为数据存储空间
	*SCSR1=0x81FE;				// CLKIN=10M,CLKOUT=4*CLKIN=40M
	*WDCR=0x0E8;				// 不使能看门狗,因为SCSR2中的WDOVERRIDE
		     					// 即WD保护位复位后的缺省值为1,故可以用
		     					// 软件禁止看门狗	     					
	//*IMR=0X0010;                //开SCI中断	     					
    *IFR=0x0ffff;                 //清中断标志
    *MCRA=0x03;             //设置IOPA2,3,4为基本IO
    *PADATDIR=0x0d00;   // 设置IOPA0为输出,IOPA1为输入
   
} 
/*************************************/
int SCIinit()
{
 *SCICCR=0x07;           		//0x17自测试模式,1个停止位,不使能奇偶校验
                                // 空闲线多处理器模式,8位字符
 *SCICTL1=0x13;         	//#0013H 使能接收和发送,SLEEP=0 禁止休眠
                            //方式 , 禁止接收错误中断,TXWAKE=0 即没
                            //有选定的发送特征
 *SCICTL2=0x03;          	//使能接收和发送中断
 *SCIHBAUD=0x04;
 *SCILBAUD=0x10;         	//波特率=4800B/S   40M/4800*8-1
 *SCICTL1=0x33;          	//使SCI脱离复位状态
 *SCIPRI=0x60;           	//SCI中断(接收和发送中断)为高优先级中断
// *MCRA=0x3;
// *PADATDIR=0x100;
	*IMR=0x10;			    //使能UART中断-INT5
	 enable();			    		//使能总中断
	*SCITXBUF='';
}
void   UartSent()          	//发送服务程序
{
	uchar  chater[]={"I am not"};
	static  int  ia=0;
	if(ia>7)      //7是字符长度,如果需要发送的字符都已经发送完,则中断直接返回
	{
	 return;
	}	
	*SCITXBUF=chater[ia++];	//依次发送定义的字符串中的各个字符
	*IFR=0x0010; 			//清除IFR中相应的中断标志 
	
	enable();				//开总中断,因为一进入中断服务程序总中断就自动关闭了
} 
 
void   UartRec()          		//接收服务程序
{                      
  receive[jj++] = *SCIRXBUF;//依次接收字符 
  if(jj>=9) jj=0;            //关闭所有中断
  *IFR= 0x0010;			//清除IFR中相应的中断标志 
	enable();				//开总中断,因为一进入中断服务程序总中断就自动关闭了
}

void interrupt SCINT()
{ 
  if(*PVIR==6)			//根据中断向量寄存器PVIR的值区别是接收还是发送中断
    UartRec();	    //如果PIVR=6,则发生了接收中断,执行接收服务程序
  if(*PVIR==7)
    UartSent();	//如果PIVR=7,则发生了发送中断,执行发送服务程序
}

  
/**************************************/
void bit8_serial_input(uchar ix)
{
uchar i,j,nc;
*PADATDIR=*PADATDIR&0xfffb;//P_E=0;    //clk=0;//IOPA2
/*ab=ix;
for(i=0;i<8;i++){
	P_RW=abit7;    //din=abit7;
	ab<<=1;
	*PADATDIR=*PADATDIR|0x0004;//P_E=1;
	for(j=0;j<10;j++)nc=0;
	*PADATDIR=*PADATDIR&0xfffb;//	P_E=0;
	} */
        for(i=0x80;i>=0x01;i=i>>1)
    {
      if((ix&i)==0) *PADATDIR=*PADATDIR&0xfff7;//P_RW=0; 
          else *PADATDIR=*PADATDIR|0x0008;//P_RW=1;
               *PADATDIR=*PADATDIR|0x0004;//P_E=1;
	  	for(j=0;j<10;j++)
        	nc=0;
	  *PADATDIR=*PADATDIR&0xfffb;//	P_E=0;
    }
}
/*-----------MCU写指令到指令寄存器IR--------------*/
void instruct(uchar dat)  //只有3个指令RS,RW不同时为0
{
 uchar ch;
*PADATDIR=*PADATDIR|0x0010;//P_RS=1;   //选通
ch=0xf8;
bit8_serial_input(ch); //和单片机进行同步5个1;
ch=dat&0xf0; //写入高四位
bit8_serial_input(ch);
ch=dat<<4; ///写低四位
bit8_serial_input(ch);
*PADATDIR=*PADATDIR&0xffef;//P_RS=0;  //结束作业
}
/*-------------MCU写数据到数据寄存器DR--------------*/
void write_abyte(uchar dat)
{
 uchar ch;
*PADATDIR=*PADATDIR|0x0010;//P_RS=1;  //读数据--不选通
ch=0xfa;  //RS=1 RW=0;
bit8_serial_input(ch);
ch=dat&0xf0;  //RS=1 RW=0;
bit8_serial_input(ch);
ch=dat<<4;
bit8_serial_input(ch);
*PADATDIR=*PADATDIR&0xffef;//P_RS=0;  //结束作业
}
/*---------写一个汉字到x,Y坐标处其中X为8列y为4行-------*/
void  write_word(uint w)//两个字节;
{
uchar ch,cl;
ch=w>>8;//高字节
cl=w;//低字节
write_abyte(ch);
write_abyte(cl);
}
/**************************************/
void dis_word(uchar x,uchar y,uchar *word)
{
 uchar i,j,t;
 instruct(home);
  if(x==1||x==3){
  if(x==3){
  x=1;
  y=y+8;
  }
  t=(y-1);
 instruct(t|0x80);
 for(i=0;i<=x;i++)
 for(j=0;j<y;j=j+16)
 write_abyte(word[i+j]);
 }
  if(x==2||x==4){
  if(x==4){
  x=2;
  y=y+8;
  }
  t=(y-1);
 instruct(t|0x90);
 for(i=0;i<=x;i++)
 for(j=0;j<y;j=j+15)
 write_abyte(word[i+j]);
 }
}
/************************************************************/
void dis_word_group(uchar x,uchar y,uchar *word,uchar number)
{
 uchar i,j,t,nu;
 instruct(home);
  if(x==2||x==4){
  if(x==4){
  x=2;
  y=y+8;
  }
  t=(y-1);
 instruct(t|0x90);
 for(nu=0;nu<number;nu++,word=word+2)
 for(i=0;i<=x;i++)
 {for(j=0;j<y;j=j+16)
 write_abyte(word[i+j]);}
 }
  if(x==1||x==3){
  if(x==3){
  x=1;
  y=y+8;
  }
  t=(y-1);
 instruct(t|0x80);
 for(nu=0;nu<number;nu++,word=word+2)
 for(i=0;i<=x;i++)
 for(j=0;j<y;j=j+16)
 write_abyte(word[i+j]);
 }
}
/*---------------确定显示位置和写入显示内容-----------------*/
dis_hz(uchar x, uchar y, uchar *p_hz)
{
 uchar loc;
 uchar hz;
 uchar xline[5]={0,1,3,2,4};
x=xline[x];
instruct(home);
loc=((x<<3)+y-9)|0x80; //LOC中是显示资料RAM
instruct(loc);//设定DDRAM位址(80H-9FH)
hz=*p_hz;
write_word(hz); //显示固定单个汉字或(双字符)单位
}
/*----------显示字母数字在x,y处就是单字节的ASCII字符-----------*/
void dis_str(uchar x, uchar y, uchar *str)
{
 uchar loc,*p;
 uchar xline[5]={0,1,3,2,4};
p=str;
x=xline[x];
instruct(home);
//loc=((x<<3)+y-9);//注意两者不一样;
loc=(16*(x-1)+y-1);
if(loc&0x01)//是否可以说是奇数,每个显示RAM的第二位
       {loc/=2;
       instruct(loc|0x80);
       //ch=' ';   //字符串前加空格(0x20 )
       write_abyte(' ');
       }else{
       loc/=2;
       instruct(loc|0x80);//每个显示RAM的第一位
       }
while(*p){
       write_abyte(*p++);
       }
}
/*--------对液晶显示屏初始化--------------------*/
void initial(void)
{
 uchar i,j;
*PADATDIR=*PADATDIR&0xffef;//P_RS=0;  //串行不选通
*PADATDIR=*PADATDIR&0xfffb;//P_E=0;   //串行方式
instruct(f_set);    //0x30;     //设定使用基本指令集动作,有11条指令
for(i=0;i<30;i++){}
for(i=0;i<250;i++){}
for(i=0;i<250;i++){}
instruct(f_set);
for(i=0;i<30;i++){}
for(i=0;i<250;i++){}
for(i=0;i<250;i++){}
instruct(dis_on);     //0x0f;     //开启屏幕 光标在第一位闪烁
for(i=0;i<30;i++){}
instruct(clear);     //0x01;     //清屏幕 clear
for(j=0;j<10;j++){
   for(i=0;i<250;i++){}   
   }
instruct(in_mode);     //0x06;    //光标右移、地址增1 方式
for(i=0;i<30;i++){}
instruct(home);
}
void delay(uint kk)
{
uint i,j;
 for(i=0;i<kk;i++)
 for(j=0;j<100;j++);
}
/*-----------------------------------*/
void main(void)
{
 uchar aa[]={"wait"},bb[]={"000"};
 uchar cc[]={"我的"},dd[]={"的"};
 jj=0;
 disable();      		    	//禁止所有中断 
 DSP_initial();
 SCIinit();                                
 dis_str(2,2,aa);
 instruct(0x0c);
 dis_word(4,2,bb);
 while(1)
 {
   /*if(jj>=1) {
     dis_str(1,1,receive);
     dis_str(2,2,bb);
     } */
 }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -