这是个测试电阻的小电路,目前遇到问题:可以成功下载,但是程序有时候跑,有时候不跑(我在while函数中写P1=0x55,用万用表测P1.0---P1.7应该为5v,0v,5v,0v,5v,0v,5v,0v,)
程序如下
/////////////////////////////////////
// Generated Initialization File //
/////////////////////////////////////
#include "compiler_defs.h"
#include "C8051F500_defs.h"
#include "stdio.h"
// Peripheral specific initialization functions,
// Called from the Init_Device() function
#define uint8 unsigned char
#define uint16 unsigned int
#define int16 int
#define uint32 unsigned long
xdata unsigned int zhjg[24]; //转换结果寄存器
int i=0;
/************************************************************
函数名称:void UART_Init ()
入口参数:无
返回参数:无;
函数说明:初始化串口,使用波特率发生器,波特率9600.
************************************************************/
void UART_Init()
{
SCON0 = 0x10;
SFRPAGE = CONFIG_PAGE;
SBRLL0 = 0x1E;
SBRLH0 = 0xFB;
SBCON0 = 0x43;
SFRPAGE = ACTIVE_PAGE;
TI0=1;
}
/************************************************************
函数名称:void ADC_Init ()
入口参数:无
返回参数:无;
函数说明:初始ADC,P1.0口为第一输入口 ;数据采用右对齐,出发模式为:突发模式
************************************************************/
void ADC_Init()
{
ADC0MX = 0x08;
ADC0CN = 0xC0;
}
/************************************************************
函数名称:void Port_IO_Init ()
入口参数:无
返回参数:无;
函数说明:初始化端口
************************************************************/
void Port_IO_Init()
{
// P0.0 - Skipped, Push-Pull, Analog
// P0.1 - Unassigned, Open-Drain, Digital
// P0.2 - Unassigned, Open-Drain, Digital
// P0.3 - Unassigned, Open-Drain, Digital
// P0.4 - UART_TX (UART0), Push-Pull, Digital
// P0.5 - UART_RX (UART0), Open-Drain, Digital
// P0.6 - Unassigned, Open-Drain, Digital
// P0.7 - Unassigned, Open-Drain, Digital
// P1.0 - Unassigned, Push-Pull, Digital
// P1.1 - Unassigned, Push-Pull, Digital
// P1.2 - Unassigned, Push-Pull, Digital
// P1.3 - Unassigned, Push-Pull, Digital
// P1.4 - Unassigned, Push-Pull, Digital
// P1.5 - Unassigned, Push-Pull, Digital
// P1.6 - Unassigned, Push-Pull, Digital
// P1.7 - Unassigned, Push-Pull, Digital
// P2.0 - Unassigned, Push-Pull, Digital
// P2.1 - Unassigned, Push-Pull, Digital
// P2.2 - Unassigned, Push-Pull, Digital
// P2.3 - Unassigned, Push-Pull, Digital
// P2.4 - Unassigned, Push-Pull, Digital
// P2.5 - Unassigned, Push-Pull, Digital
// P2.6 - Unassigned, Push-Pull, Digital
// P2.7 - Unassigned, Push-Pull, Digital
// P3.0 - Unassigned, Push-Pull, Analog
// P3.1 - Unassigned, Push-Pull, Analog
// P3.2 - Unassigned, Push-Pull, Analog
// P3.3 - Unassigned, Push-Pull, Analog
// P3.4 - Unassigned, Push-Pull, Analog
// P3.5 - Unassigned, Push-Pull, Analog
// P3.6 - Unassigned, Push-Pull, Analog
// P3.7 - Unassigned, Push-Pull, Analog
// P4.0 - Unassigned, Open-Drain, Digital [Fixed]
// P4.1 - Unassigned, Open-Drain, Digital [Fixed]
// P4.2 - Unassigned, Open-Drain, Digital [Fixed]
// P4.3 - Unassigned, Open-Drain, Digital [Fixed]
// P4.4 - Unassigned, Open-Drain, Digital [Fixed]
// P4.5 - Unassigned, Open-Drain, Digital [Fixed]
// P4.6 - Unassigned, Open-Drain, Digital [Fixed]
// P4.7 - Unassigned, Open-Drain, Digital [Fixed]
SFRPAGE = CONFIG_PAGE;
P0MDIN = 0xFE;
P3MDIN = 0x00;
P0MDOUT = 0x11;
P1MDOUT = 0xFF;
P2MDOUT = 0xFF;
P3MDOUT = 0xFF;
P0SKIP = 0x01;
XBR0 = 0x01;
XBR2 = 0x40;
SFRPAGE = ACTIVE_PAGE;
}
/************************************************************
函数名称:void Oscillator_Init ()
入口参数:无
返回参数:无;
函数说明:初始化内部晶振,24MHZ
************************************************************/
void Oscillator_Init()
{
SFRPAGE = CONFIG_PAGE;
OSCICN = 0xC7;
SFRPAGE = ACTIVE_PAGE;
}
/************************************************************
函数名称:void PCA_Init ()
入口参数:无
返回参数:无;
函数说明:初始化看门狗,关闭看门狗
************************************************************/
void PCA_Init()
{
PCA0MD &= ~0x40;
PCA0MD = 0x00;
}
/************************************************************
函数名称:void Timer_Init ()
入口参数:无
返回参数:无;
函数说明:初始化定时器
************************************************************/
/*
void Timer_Init()
{
TMOD = 0x22;
CKCON = 0x02;
TH0 = 0x2B;
TH1 = 0x59;
}
*/
/************************************************************
函数名称:void Send (uint8 i)
入口参数: i
返回参数:无;
函数说明: 发送8位数据
************************************************************/
void Send(uint8 i)
{
SBUF0 = i;
while(TI0==0)
TI0 = 0;
}
/*
void Timer_Interrupt(void) interrupt 3
{
TF1=0;
if(i==0)
{
P1=0x55;
i=1;
}
else if(i==1)
{
P1=0xaa;
i=0;
}
}
*/
/************************************************************
函数名称:void UART0_Interrupt()
入口参数:无
返回参数:无;
函数说明:串口中断函数
************************************************************/
/*
void UART0_Interrupt (void) interrupt 4
{
int i,j;
int limit;
unsigned char xdata s_buf[8];
for(i=0;i<8;i++)
{
for(limit=0;limit < 5000;limit++)
{
if(RI0)
{
limit=0;
break;
}
}
RI0 =0;
s_buf[i]=SBUF0;
if(s_buf[0] != 0x00)
return;
}
if(s_buf[0] == 0x00 && s_buf[1] == 0x01)
{
j=zhjg[0]>>8;
Send(j);
j=zhjg[0];
Send(j);
}
if(s_buf[0] == 0x00 && s_buf[1] == 0x02)
{
j=zhjg[1]>>8;
Send(j);
j=zhjg[1];
Send(j);
}
}
*/
/************************************************************
函数名称:void Delay(uint16 time)
入口参数:uint16 time
返回参数:无;
函数说明:延迟函数
************************************************************/
void Delay(uint16 time)
{
while(time--)
{
NOP();
}
}
/************************************************************
函数名称:void Interrupts_Init ()
入口参数:无
返回参数:无;
函数说明:初始化中断优先级
************************************************************/
void Interrupts_Init()
{
IP = 0x02;
IE = 0x80;
}
/*禁止VDD监控复位*/
void Reset_Sources_Init()
{
VDM0CN = 0x00;
}
// Initialization function for device,
// Call Init_Device() from your main program
void Init_Device(void)
{
PCA_Init();
Reset_Sources_Init();
// Timer_Init();
UART_Init();
ADC_Init();
Port_IO_Init();
Oscillator_Init();
Interrupts_Init();
}
/************************************************************
函数名称:void adc ()
入口参数:无
返回参数:无;
函数说明:AD转换,突发模式,每个通道连续转换4次,转换数据右对齐
************************************************************/
void adc(void)
{
uint8 i;
uint16 result;
for(i=0;i<24;i++)
{
ADC0MX = 0x08+i; //P1.0为第一通道
AD0BUSY = 1;
while(AD0INT);//查询转换结束标志
result =ADC0H;
result =result<<8;
result |=ADC0L;
zhjg[i]=result;
AD0INT =0;
}
}
/************************************************************
函数名称:void main()
入口参数:无
返回参数:无;
函数说明:主函数
************************************************************/
void main()
{
// RSTSRC=0x10;
Delay(1000);
Init_Device();
Delay(1000);
while(1)
{
// adc();
Delay(1000);
P1=0x55;
// Delay(1000);
// Delay(1000);
// Delay(1000);
// P1=0xaa ;
// Delay(1000);
// Delay(1000);
// Send(0x55);
}
}
是复位问题还是程序初始化编写问题?求高手指点 |