用F28027把基本的程序写出来了,按着时序一步步。请各位高手指点,有无改进的地方。
#include "DSP28x_Project.h" // Device Headerfile and Examples Include File
#define unchar unsigned char
/// Select the example to compile in. Only one example should be set as 1
// the rest should be set as 0.
void Gpio_select1(void);
void Gpio_select2(void);
void RH(void);
unchar COM(void);
//interrupt void cpu_timer0_isr(void);
typedef unsigned char U8; /* defined for unsigned 8-bits integer variable 无符号8位整型变量 */
U8 U8temp;
U8 flag;
U8 U8T_data_H,U8T_data_L,U8RH_data_H,U8RH_data_L,U8checkdata;
U8 U8T_data_H_temp,U8T_data_L_temp,U8RH_data_H_temp,U8RH_data_L_temp,U8checkdata_temp;
U8 U8comdata;
void main(void)
{
// Step 1. Initialize System Control:
// PLL, WatchDog, enable Peripheral Clocks
// This example function is found in the DSP2802x_SysCtrl.c file.
InitSysCtrl();
// Step 2. Initalize GPIO:
// This example function is found in the DSP2802x_Gpio.c file and
// illustrates how to set the GPIO to it's default state.
// InitGpio(); // Skipped for this example
// For this example use the following configuration:
// Gpio_select();
// Step 3. Clear all interrupts and initialize PIE vector table:
// Disable CPU interrupts
DINT;
// Initialize PIE control registers to their default state.
// The default state is all PIE interrupts disabled and flags
// are cleared.
// This function is found in the DSP2802x_PieCtrl.c file.
InitPieCtrl();
// Disable CPU interrupts and clear all CPU interrupt flags:
IER = 0x0000;
IFR = 0x0000;
// Initialize the PIE vector table with pointers to the shell Interrupt
// Service Routines (ISR).
// This will populate the entire table, even if the interrupt
// is not used in this example. This is useful for debug purposes.
// The shell ISR routines are found in DSP2802x_DefaultIsr.c.
// This function is found in DSP2802x_PieVect.c.
InitPieVectTable();
// Step 4. Initialize all the Device Peripherals:
// This function is found in DSP2802x_InitPeripherals.c
// InitPeripherals(); // Not required for this example
EALLOW; // This is needed to write to EALLOW protected registers
PieVectTable.TINT0 = &cpu_timer0_isr;
EDIS; // This is needed to disable write to EALLOW protected registers
InitCpuTimers();
// 100MHz CPU Freq, 1 second Period (in uSeconds)
ConfigCpuTimer(&CpuTimer0, 60, 1000000);//100
StartCpuTimer0();
// Enable CPU INT1 which is connected to CPU-Timer 0:
IER |= M_INT1;
// Enable TINT0 in the PIE: Group 1 interrupt 7
PieCtrlRegs.PIEIER1.bit.INTx7 = 1;
// Enable global Interrupts and higher priority real-time debug events:
EINT; // Enable Global interrupt INTM
ERTM; // Enable Global realtime interrupt DBGM
for(;;);
}
void delay_loop0() //执行每条指令 225ns
{
unsigned int i,j;
for (i = 0; i<80; i++)
{
for(j=0;j<1000;j++);
}
}
void delay_loop1() //执行每条指令 225ns
{
unsigned int i,j;
for (i = 0; i<9; i++)
{
for(j=0;j<10;j++);
}
}
void delay_loop4() //执行每条指令 225ns
{
unsigned int i,j;
for (i = 0; i<13; i++)
{
for(j=0;j<10;j++);
}
}
void Gpio_select1(void)
{
EALLOW;
GpioCtrlRegs.GPAMUX1.all = 0x00000000; // All GPIO
GpioCtrlRegs.GPADIR.bit.GPIO6=1; // All outputs //若等于GpioCtrlRegs.GPADIR.bit.GPIO6=1
EDIS;
}
void Gpio_select2(void)
{
EALLOW;
GpioCtrlRegs.GPAMUX1.all = 0x00000000; // All GPIO
GpioCtrlRegs.GPADIR.bit.GPIO6=0; //
// GpioCtrlRegs.GPBDIR.all = 0x0000000F; // All outputs
EDIS;
}
unchar COM()
{
int q;
for(q=0;q<8;q++)
{
// temp=temp<<1;
flag=2;
while((GpioDataRegs.GPADAT.bit.GPIO6)==0);
delay_loop4(); //延时29.25us,判断接受的是 0 还是 1
U8temp=0;
if((GpioDataRegs.GPADAT.bit.GPIO6)!=0)
U8temp=1;
flag=2;
while((GpioDataRegs.GPADAT.bit.GPIO6)&&flag++);
if(flag==1)
break;
U8comdata<<=1;
U8comdata|=U8temp;
}
return U8comdata;
}
void RH(void)
{
Gpio_select1();
GpioDataRegs.GPASET.bit.GPIO6=0;
delay_loop0(); //拉低18ms
GpioDataRegs.GPASET.bit.GPIO6=1;
delay_loop1(); //拉高20.25us
//设主机为输入,判断DHT响应信号
Gpio_select2();
if(!(GpioDataRegs.GPADAT.bit.GPIO6))
{
flag=2;
//判断从机是否发出 80us 的低电平响应信号是否结束
while((!(GpioDataRegs.GPADAT.bit.GPIO6))&&flag++);
flag=2;
//判断从机是否发出80us的高电平,如发出则进入数据接收状态
while((GpioDataRegs.GPADAT.bit.GPIO6)&&flag++);
COM();
U8RH_data_H_temp=U8comdata;
COM();
U8RH_data_L_temp=U8comdata;
COM();
U8T_data_H_temp=U8comdata;
COM();
U8T_data_L_temp=U8comdata;
COM();
U8checkdata_temp=U8comdata;
//难道还要切换输入输出方式么?
Gpio_select1();
GpioDataRegs.GPASET.bit.GPIO6=1;
//数据校验
U8temp=(U8T_data_H_temp+U8T_data_L_temp+U8RH_data_H_temp+U8RH_data_L_temp);
if(U8temp==U8checkdata_temp)
{
U8RH_data_H=U8RH_data_H_temp;
U8RH_data_L=U8RH_data_L_temp;
U8T_data_H=U8T_data_H_temp;
U8T_data_L=U8T_data_L_temp;
U8checkdata=U8checkdata_temp;
}//fi
}
}
interrupt void cpu_timer0_isr(void)
{
CpuTimer0.InterruptCount++;
//调用温湿度读取子程序
RH();
//if(CpuTimer0.InterruptCount*100>10000)CpuTimer0.InterruptCount=0;
// Acknowledge this interrupt to receive more interrupts from group 1
PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;
} |