MSP430单片机驱动
#include "AT21CS01.h"
#include "delay.h"
#include <msp430x24x.h>
#include "stdio.h"
#include <string.h>
u8 at21cs01_slaveAddress=0;
u8 CommSpeed=0;
void at21cs01_delay_us (u16 us)
{
u16 t;
for(t=0;t<us;t++)
{
delay_us(1);
}
}
void at21cs01_delay_ms (u16 ms)
{
u16 t;
for(t=0;t<ms;t++)
{
delay_ms(1);
}
}
void at21cs01_SIO_SetHigh()
{
AT21CS01_IO_H;
}
void at21cs01_SIO_SetLow()
{
AT21CS01_IO_L;
}
u8 at21cs01_SIO_GetValue()
{
u8 dat;
AT21CS01_IO_Output;
AT21CS01_IO_L;
delay_us(1);
AT21CS01_IO_Input;
delay_us(1);
if(GPIO_ReadInputDataBit == 0x40)
dat = 1;
else
dat = 0;
//delay_us(14);
AT21CS01_IO_Output;
AT21CS01_IO_H;
delay_us(14);
return dat;
}
u8 at21cs01_ackNack()
{
u8 temp;
if (CommSpeed == 0)
{
//AT21CS01_IO_Output;
AT21CS01_IO_L;
delay_us(1);
AT21CS01_IO_Input;
delay_us(1);
if(GPIO_ReadInputDataBit == 0x40)
temp = 0x00;
else
temp = 0x01;
//delay_us(14);
AT21CS01_IO_Output;
AT21CS01_IO_H;
delay_us(14);
}
else{
AT21CS01_IO_L;
delay_us(4);
AT21CS01_IO_Input;
delay_us(2);
if(GPIO_ReadInputDataBit == 0x40)
temp = 0x00;
else
temp = 0x01;
delay_us(34);
AT21CS01_IO_Output;
AT21CS01_IO_H;
}
return temp;
}
u8 at21cs01_discoveryResponse()
{
u8 temp;
at21cs01_SIO_SetHigh();
at21cs01_delay_us(200);
at21cs01_SIO_SetLow();
at21cs01_delay_us(500);
at21cs01_SIO_SetHigh();
at21cs01_delay_us(20);
at21cs01_SIO_SetLow();
at21cs01_delay_us(1);
at21cs01_SIO_SetHigh();
at21cs01_delay_us(2);
if(at21cs01_SIO_GetValue() == 0){ temp = 0x01; }
else { temp = 0x00; }
at21cs01_delay_us(21);
at21cs01_SIO_SetHigh();
CommSpeed = 0;
return temp;
}
u8 at21cs01_Init()
{
u8 err;
AT21CS01_IO_Output;
AT21CS01_IO_H;
err=at21cs01_discoveryResponse();
return err;
}
void at21cs01_startHS()
{
at21cs01_SIO_SetHigh();
if (CommSpeed == 0)
{
at21cs01_delay_us(200);
}
else
{
at21cs01_delay_us(650);
}
}
void at21cs01_tx1()
{
if (CommSpeed ==0)
{
at21cs01_SIO_SetLow();
at21cs01_delay_us(1);
at21cs01_SIO_SetHigh();
at21cs01_delay_us(15);
}
else
{
at21cs01_SIO_SetLow();
at21cs01_delay_us(4);
at21cs01_SIO_SetHigh();
at21cs01_delay_us(41);
}
}
void at21cs01_tx0()
{
if (CommSpeed == 0)
{
at21cs01_SIO_SetLow();
at21cs01_delay_us(8);
at21cs01_SIO_SetHigh();
at21cs01_delay_us(8);
}
else
{
at21cs01_SIO_SetLow();
at21cs01_delay_us(24);
at21cs01_SIO_SetHigh();
at21cs01_delay_us(21);
}
}
u8 at21cs01_txByte(u8 dataByte)
{
u8 temp;
AT21CS01_IO_Output;
for (u8 ii = 0; ii < 8 ; ii++)
{
if (0x80 & dataByte)
{
at21cs01_tx1();
}
else
{
at21cs01_tx0();
}
dataByte <<= 1;
}
temp = at21cs01_ackNack();
//printf("at21cs01_ackNack = %d ",temp);
return temp;
}
u8 at21cs01_readByte ()
{
u8 temp;
u8 dataByte = 0x00;
if (CommSpeed == 0)
{
for (u8 ii = 0; ii < 8 ; ii++)
{
temp = at21cs01_SIO_GetValue();
dataByte = (u8)((dataByte << 1) | temp);
at21cs01_delay_us(9);
}
}
else
{
for (u8 ii = 0; ii < 8 ; ii++)
{
temp = at21cs01_SIO_GetValue();
dataByte = (u8)((dataByte << 1) | temp);
at21cs01_delay_us(34);
}
}
return dataByte;
}
u8 at21cs01_scanDeviceAddress()
{
u8 temp=0;
u8 address=0;
for(u8 ii =0; ii < 8; ii++)
{
if(at21cs01_discoveryResponse() == 0)
{
at21cs01_startHS();
temp = ii;
temp <<= 1;
temp |= 0xA0;
if(at21cs01_txByte(temp) == 0x00)
{
//printf("发送成功 ");
address = ii;
address <<= 1;
at21cs01_startHS();
break;
}
}
}
if(1)
{
printf("Slave Address = ");
if (address == 0x00) {printf("000b\n");}
if (address == 0x02) {printf("001b\n");}
if (address == 0x04) {printf("010b\n");}
if (address == 0x06) {printf("011b\n");}
if (address == 0x08) {printf("100b\n");}
if (address == 0x0A) {printf("101b\n");}
if (address == 0x0C) {printf("110b\n");}
if (address == 0x0E) {printf("111b\n");}
}
at21cs01_slaveAddress=address;
return address;
}
u8 at21cs01_eepromwrite(u8 cmd, u8 address, u8 Data[],u8 size)
{
unsigned char temp=0x01;
if(size>8)return 1;
//cmd |=at21cs01_slaveAddress;
at21cs01_startHS();
temp &=at21cs01_txByte(cmd);
temp &=at21cs01_txByte(address);
for (u8 ii = 0; ii < size; ii++)
{
temp &=at21cs01_txByte(Data[ii]);
}
at21cs01_startHS();
at21cs01_delay_ms(5);
return temp;
}
u8 at21cs01_WriteToAddress(u8 address, u8 Data[],u8 size)
{
return at21cs01_eepromwrite(AT21CS01_CMD_EEPROMREADWRITE,address,Data,size);
}
u8 at21cs01_ReadFromAddress(u8 address, u8 *readData, u8 size)
{
u8 temp=0x01;
u8 cmd=0xA0;
//cmd |=at21cs01_slaveAddress;
at21cs01_startHS();
temp &=at21cs01_txByte(cmd);
temp &=at21cs01_txByte(address);
at21cs01_startHS();
temp &=at21cs01_txByte((u8)(cmd | 0x01));
for (u8 ii = 0; ii < size; ii++)
{
readData[ii] = at21cs01_readByte();
if (ii < (size-1))
{
at21cs01_tx0();
}
}
at21cs01_tx1();
at21cs01_startHS();
if(1)
{
printf(" Data Read =");
for (u8 ii = 0; ii < size; ii++)
{
//printf(" 0x");
printf("%d \n", readData[ii]);
}
printf("\n");
}
return temp;
}
void at21cs01_currentRead(u8 cmd, u8 *readData, u8 size)
{
cmd |= at21cs01_slaveAddress;
at21cs01_startHS();
at21cs01_txByte((u8)(cmd | 0x01));
for (u8 ii = 0; ii < size; ii++)
{
readData[ii] = at21cs01_readByte();
if (ii < (size-1))
{
at21cs01_tx0();
}
}
at21cs01_tx1();
at21cs01_startHS();
if(1)
{
printf(" Current Address =");
for (u8 ii = 0; ii < size; ii++)
{
//printf(" 0x");
printf("%d", readData[ii]);
}
printf("\n");
}
}
u8 at21cs01_setCommuncationSpeed(u8 speed)
{
u8 temp;
speed |= at21cs01_slaveAddress;
at21cs01_startHS();
temp = at21cs01_txByte((u8)(speed));
at21cs01_startHS();
if (((speed & 0xF0) == AT21CS01_SPEED_SLOW) & (temp == 0x00))
{
CommSpeed = 1;
}
else
{
CommSpeed = 0;
}
return temp;
}
u8 at21cs01_checkCommuncationSpeed(u8 speed)
{
u8 temp;
speed |= at21cs01_slaveAddress;
at21cs01_startHS();
temp = at21cs01_txByte((u8)(speed|0x01));
at21cs01_startHS();
if(temp == 0x00){
if((speed & 0xF0) == AT21CS01_SPEED_SLOW){
printf(" Standard Speed ACK\n");
}
if((speed & 0xF0) == AT21CS01_SPEED_FAST){
printf(" High-Speed ACK\n");
}
}
else {
if((speed & 0xF0) == AT21CS01_SPEED_SLOW){
printf(" Standard Speed NACK\n");
}
if((speed & 0xF0) == AT21CS01_SPEED_FAST){
printf(" High-Speed NACK\n");
}
}
return temp;
}
void at21cs01_FillWholeMemory(u8 byte)
{
u8 data[128];
memset(data,byte,128);
u8 addr=0x00;
for(u8 z=0;z<16;z++)
{
at21cs01_eepromwrite(AT21CS01_CMD_EEPROMREADWRITE,addr,data,8);
addr+=8;
}
}
void at21cs01_Connect()
{
at21cs01_slaveAddress = at21cs01_scanDeviceAddress();
//at21cs01_setCommuncationSpeed(at21cs01_Settings->prefCommunicationSpeed);
//if(debug==1)
//at21cs01_checkCommuncationSpeed(at21cs01_Settings->prefCommunicationSpeed);
}
//Ack_t AT21CS01_Reset(void)
//{
// u8 pulse_time=0;
// AT21CS01_IO_Output;
// AT21CS01_IO_L;
// delay_us(600);//在器件上电后或主器件复位器件(将 SI/O 线保持低电平并持续 tRESET或 tDSCHG)后
// AT21CS01_IO_H;//主器件必须释放SI/O 线,该线随后将通过外部上拉电阻拉为高电平
// delay_us(15); // 主器件必须再等待一段最小 tRRT 时间,然后才能请求从器件获取发现响应应答
// AT21CS01_IO_L;//在发现响应应答序列中,首先是主器件将 SI/O 线驱动为低电平
// delay_us(1);//T-MSDR 2~6us主器件必须继续将此线驱动为低电平并持续 tDRR
//
// AT21CS01_IO_Input;//主器件释放总线,准备采样从器件应答信号
// //delay_us(1);
// while((GPIO_ReadInputDataBit==0)&&pulse_time<10)//这里来获得低电平的持续时间,来确定器件是否响应正常
// {//在 tDRR 时间内,AT21CS01/11 将以并发的方式将 SI/O 驱动为低电平来进行响应
// pulse_time++;//器件会继续将 SI/O 线驱动为低电平,总时长为 tDACK。主器件应从 tDRR 开始的 tMSDR 后对 SI/O 线的状态进行采样
// delay_us(1);
// }
// delay_us(200);
// AT21CS01_IO_Output;//释放ACK结束
// AT21CS01_IO_H;
// if( pulse_time >=4 )//如果器件将 SI/O 驱动为低电平的时间大于4us则说明器件响应是正常的
// {
// return ACK;// ACK
// }
// else
// {
// return NACK; //NACK
// }
//}
//
//void AT21CS01_Start(void)
//{
// AT21CS01_IO_Output;
// AT21CS01_IO_H;
// delay_us(300);
//}
//
//Ack_t AT21_init(void)
//{
// Ack_t err;
// AT21CS01_IO_Output;
// AT21CS01_IO_H;
//
// at21cs01_discoveryResponse();
// return err;
//}
//
//}
//
//
#define REVERSE8(data) \
{ \
data = ((data & 0xF0) >> 4) | ((data & 0x0f) << 4); \
data = ((data & 0xCC) >> 2) | ((data & 0x33) << 2); \
data = ((data & 0xAA) >> 1) | ((data & 0x55) << 1); \
}
/*序列号最后1byte为前7byte的CRC*/
u8 CRC8_MAXIM(u8* input, u16 num)
{
u8 data = 0, crc = 0x00;
u16 i = 0;
for (; num > 0; num--)
{
data = *input++;
REVERSE8(data);
crc = crc ^ data;
for (i = 0; i < 8; i++)
{
if (crc & 0x80)
crc = (crc << 1) ^ 0x31;
else
crc <<= 1;
}
}
REVERSE8(crc);
crc = crc ^ 0x00;
return crc;
}
#ifndef __AT21CS01_H
#define __AT21CS01_H
#define u8 unsigned char
#define uint unsigned int
#define ulong long int
#define u16 unsigned short int
#define AT21CS01_SPEED_SLOW 0xD0
#define AT21CS01_SPEED_FAST 0xE0
#define AT21CS01_CMD_EEPROMREADWRITE 0xA0
#define AT21CS01_IO_Output P2DIR|=BIT6 //output
#define AT21CS01_IO_Input P2DIR&=~BIT6 ; //input
#define AT21CS01_IO_H P2OUT|=BIT6 //set
#define AT21CS01_IO_L P2OUT&=~BIT6 //reset
#define GPIO_ReadInputDataBit (P2IN&BIT6)
u8 at21cs01_Init();
u8 at21cs01_discoveryResponse();
u8 at21cs01_scanDeviceAddress();
void at21cs01_FillWholeMemory(u8 byte);
u8 at21cs01_ReadFromAddress(u8 address, u8 *readData, u8 size);
u8 at21cs01_eepromwrite(u8 cmd, u8 address, u8 Data[],u8 size);
#endif