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

📄 main.c

📁 C8051F226单片机的初始化
💻 C
字号:
#include <c8051F340.h>
//#include <stdio.h>
//#include <absacc.h>
//#include <math.h>
#include <intrins.h>
#include "ZLG7290.h"
#include "I2C.h"	

#define uchar unsigned char 
#define uint unsigned int

//sbit LedLink = P4^7;   //P4口不可位寻址
#define LedLink  1 << 7

volatile bit FlagINT = 0;


unsigned char Read_Buffer[10];
unsigned char Write_Buffer[10] ={0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0xFF};
/*---------------------------------------------
函数名:INT1_Init
功  能:外部中断1初始化
参  数:无
返回值:无
修改记录
----------------------------------------------
日期     作者      内容

---------------------------------------------*/
void INT1_Init(void)
{
 IT01CF |= 0x40;//INT1连接到P0.4  低电平触发中断 

 IT1 = 0x00;    //低电平触发中断
 EX1 = 0x01;    //允许外部中断 
}


//*******************************************************************
//函数:INT0_SVC()
//功能:ZLG7290键盘中断服务程序
//说明:中断触发方式选择负边沿触发,因此不必等待中断请求信号恢复为高电平
//********************************************************************
void INT1_ISR(void) interrupt 2 using 1
{
 FlagINT = 1;
}

/*---------------------------------------------
函数名:deley_us
功  能:延时us级子程序
参  数:unsigned char Nus
返回值:无
修改记录
----------------------------------------------
日期     作者      内容

---------------------------------------------*/
void deley_us(unsigned char Nus)
{
 unsigned char i;
 for(i=0;i<Nus;i++)
 {
  _nop_();
  _nop_();
  _nop_();
  _nop_();
  _nop_();
 }
}

/*---------------------------------------------
函数名:deley_ms
功  能:延时ms级子程序
参  数:unsigned char Nms
返回值:无
修改记录
----------------------------------------------
日期     作者      内容

---------------------------------------------*/
void deley_ms(unsigned char Nms)
{
 unsigned char i;
 for(i=0;i<Nms;i++)
 {
  deley_us(250);
 }
}


/*******************************************
//采用外部晶振初始化程序(11.0592MHZ)
*******************************************/
void Oscillator_Init(void)
{
    unsigned int i;

    OSCXCN = 0x67;
    for (i = 0; i < 3000; i++);  // Wait 1ms for initialization

    while ((OSCXCN & 0x80) == 0);

    RSTSRC = 0x06;  //Enable missing clock detector and VDD Monitor reset
    CLKMUL = 0x02;
    CLKSEL = 0x01;  //系统时钟选择外部振荡器 USB时钟选择外部振荡器
	OSCICN = 0x00;  //内部高频振荡器禁止
}

/**********************************************
//名称:System_init()
//功能:系统初始化函数
//输入:无
//返回:无
**********************************************/
void System_init(void)
{
    EA = 0;			  
    PCA0MD &= ~0x40; //看门狗关
    PCA0MD = 0x00;
	//WDTCN = 0x0de;
	//WDTCN = 0x0ad;  //disable WDTz

    P0MDIN = 0x03f;	  //P0.6, P0.7 are analog 
	P1MDIN = 0x0ff;
	P2MDIN = 0x0ff;
    P3MDIN = 0x0ff;
    P4MDIN = 0x0ff;   //数字输入使能

	P0MDOUT = 0x0ff;  
	P1MDOUT = 0x0ff;
	P2MDOUT = 0x0ff;
    P3MDOUT = 0x0ff;
    P4MDOUT = 0x0ff;  //IO口为推挽输出
    
    P0SKIP = 0xc0;    //P0.6, P0.7 skipped in the Crossbar 
    P1SKIP = 0x00;
    P2SKIP = 0x00;
    P3SKIP = 0x00;
    //P4SKIP = 0x00;   //引脚不被交叉开关跳过

    XBR0 = 0x08;       //Route /SYSCLK to first available pin
    XBR1 = 0x00;
    XBR2 = 0x00;
    
    XBR1 |= 0x40;     //使能交叉开关
    Oscillator_Init();//外部晶振初始化程序
    RS0=0;
    RS1=0;            //设置工作寄存器组为0.
	
	IE = 0x80;		  //开中断
}


/*******************************************
//主程序
*******************************************/
void main(void) 
{   
 unsigned char KeyValue = 0;

 System_init();
 deley_ms(250);
 deley_ms(250);
 //LedLink = 1;
 P4 &= ~LedLink;

 INT1_Init();
 I2C_Init();
 //wrnbyt(0x00,10,Write_Buffer);
 //deley_ms(250);
 //deley_ms(250);
 //rdnbyt(0x00,10,Read_Buffer);

 while(1)
 {
  if(FlagINT)		
  {//如果有键按下,产生中断	
   FlagINT = 0;  //清除中断标志
   KeyValue = ZLG7290_ReadReg(0x01);//读取键值
  }
  P4 |= LedLink;
  deley_ms(250);
  deley_ms(250);
  deley_ms(250);
  deley_ms(250);
  P4 &= ~LedLink;
  deley_ms(250);
  deley_ms(250);
  deley_ms(250);
  deley_ms(250);
  
 }
}

⌨️ 快捷键说明

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