使用msp430对ch376并口操作 读出数据始终为0xff下面是程序:
#include
#define CPU_F ((double)32768) //延时函数宏定义
#define delay_ms(x) __delay_cycles((long)(CPU_F*(double)x/31.2))
//#define delay_us(x) __delay_cycles((long)(CPU_F*(double)x/0.032))
//#define P6_OUT P6DIR = 0xff //8位并口输出
#define P5 P5DIR|=(BIT4+BIT5+BIT6|BIT7) // 控制管脚
//#define P6_IN P6DIR =0x00 //8位并口输入
#define A0_L P5OUT &=~BIT4
#define A0_H P5OUT |=BIT4
#define RD_L P5OUT &=~BIT5
#define RD_H P5OUT |=BIT5
#define WR_L P5OUT &=~BIT6
#define WR_H P5OUT|=BIT6
#define CS_L P5OUT &=~BIT7
#define CS_H P5OUT|=BIT7
#define INT P2DIR|=BIT4
unsigned char temp[2];
//****************************************
void delay(unsigned int ms )
{
unsigned int i;
while ( ms -- )
for ( i = 1000; i != 0; i -- );
}
//*******************************************
void CH376_init()
{
CS_H;
WR_H;
RD_H;
A0_L;
P6DIR =0X00; //设置 8位并行输入
}
//************并口写命令******************
void send_byte_cmd(unsigned char ch)
{
P6OUT = ch;
P6DIR = 0XFF; //设置 8位并行输出
A0_H;
CS_L;
WR_L;
CS_L;
WR_H;
CS_H;
A0_L;
P6DIR = 0X00; //设置 8位并行输入
_NOP( );_NOP( );_NOP( );_NOP( );
}
//************写数据***********************
void send_byte_date(unsigned char ch)
{
P6OUT = ch;
P6DIR = 0XFF;//设置 8位并行输出
A0_L;
CS_L;
WR_L;
CS_L;
WR_H;
CS_H;
P6DIR = 0X00;//设置 8位并行输入
}
//*************读ch376状态************************
unsigned char read_date()
{
unsigned char dat;
P6DIR = 0X00;//设置 8位并行输入
A0_H;
CS_L;
RD_L;
CS_L;
dat=P6IN; //设置 8位并行输入
RD_H;
CS_H;
A0_L;
return dat;
}
//************并口读命令数据*********************************
unsigned char read_cmd_dat()
{
unsigned char mdat;
P6DIR=0X00;
A0_L;
CS_L;
RD_L;
CS_L;
mdat=P6IN;
RD_H;
CS_H;
A0_L;
return mdat;
}
//***********初始化ch376****************************
unsigned char init()
{
unsigned res;
CH376_init();
send_byte_cmd(0x06); //写命令
send_byte_date(0x65); // 写数据
res=read_cmd_dat();
return res;
}
//*******************************************
void main(void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop WDT
UCSCTL4 |= SELA1; //选择时钟源
AUXCTL1 |= AUX1MD ; // 单片机硬件配置
AUXCTL1 &= ~AUX1OK;
AUXCTL2 |= AUX2MD ;
AUXCTL2 &= ~AUX2OK;
CH376_init();
delay_ms(50);
temp[0]=init();
delay_ms(55);
}