How to start a new MCU Controller
/******************************************************************/
Example:Use GPIO simulate I2C bus
void I2C_init(void) :Initial GPIO
void I2C_start(void) :I2C bus start
void I2C_stop(void) :I2C bus end
void I2C_send_8bit(char data) :I2C send 8 bits data
char I2C_receive_8bit(void) :I2C receive 8 bits data
/******************************************************************/
void I2C_init(void)
{
gpio_init_io(I2C_SCL, GPIO_OUTPUT); //Set GPIO Dir
gpio_init_io(I2C_SDA, GPIO_OUTPUT); //Set GPIO Dir
//stop
gpio_write_io(I2C_SCL, DATA_HIGH); //SCL=H
gpio_write_io(I2C_SDA, DATA_HIGH); //SDA=H
}
void I2C_start(void)
{
gpio_write_io(I2C_SCL, DATA_HIGH); //SCL=H
nop();
gpio_write_io(I2C_SDA, DATA_HIGH); //SDA=H
nop();
gpio_write_io(I2C_SDA, DATA_LOW); /SDA=L
}
void I2C_stop(void)
{
nop();
gpio_write_io(I2C_SDA, DATA_LOW); //SDA=L
nop();
gpio_write_io(I2C_SCL, DATA_HIGH); //SCL=H
nop();
gpio_write_io(I2C_SDA, DATA_HIGH); //SDA=H
}
void I2C_send_8bit(char data)
{
char i;
for (i=0;i<8;i++)
{
gpio_write_io(I2C_SCL, DATA_LOW); //SCL=L
nop();
if (data & 0x80)
gpio_write_io(I2C_SDA, DATA_HIGH); //SDA=H
else
gpio_write_io(I2C_SDA, DATA_LOW); //SDA=L
nop();
gpio_write_io(I2C_SCL, DATA_HIGH); //SCL=H
nop();
data <<= 1;
}
// The 9th bit transmission ACK
gpio_write_io(I2C_SCL, DATA_LOW); //SCL=L
gpio_write_io(I2C_SDA, DATA_HIGH); //SDA=H
gpio_init_io(I2C_SDA, GPIO_INPUT);
while(gpio_read_io(I2C_SDA)!=0); //wait ACK
gpio_write_io(I2C_SCL, DATA_HIGH); //SCL=H
nop();
gpio_write_io(I2C_SCL, DATA_LOW); //SCL=L
gpio_init_io(I2C_SDA, GPIO_OUTPUT);
}
char I2C_receive_8bit(void)
{
char data=0,i=0;
gpio_init_io(I2C_SDA, GPIO_INPUT);
for (i=0;i<8;i++)
{
gpio_write_io(I2C_SCL, DATA_LOW); //SCL=L
nop();
gpio_write_io(I2C_SCL, DATA_HIGH); //SCL=H
data <<=1;
data |=( gpio_read_io(I2C_SDA)); //receive bit data
nop();
}
// The 9th bit transmission
gpio_write_io(I2C_SDA, DATA_LOW);
gpio_write_io(I2C_SCL, DATA_LOW); //SCL=L
gpio_init_io(I2C_SDA, GPIO_OUTPUT);
nop();
nop();
gpio_write_io(I2C_SCL, DATA_HIGH); //SCL=H
nop();
nop();
gpio_write_io(I2C_SCL, DATA_LOW); //SCL=L
return data;
}
/******************************************************************/
Example:CRC Check
INT32U crctab[]:CRC check table have 256 set of 32 bit data to creat crc check value
int calculate_crc32(char* buf, int size)
char* buf :data address
int size : data size
resurn:crc value
/******************************************************************/
INT32U crctab[256]={ 0x00000000,0x77073096,0xee0e612c,
︴︴
, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d };
int calculate_crc32(char* buf, int size)
{
int i=0,crc = 0xffffffff;
for (i=0;i<size; i++)
crc = ((crc >> 8)&0xffffffff) ^ crctab[(crc & 0xff )^ *buf++];
crc ^= 0xffffffff ;
return crc; //return CRC valuse
}
/******************************************************************/
Example:Use GPIO simulate ISA Bus
void send_cmd(INT16U addr, INT16U data):Used ISA bus send data
INT16U read_cmd(INT16U addr):Used ISA bus read data
/******************************************************************/
void send_cmd(INT16U addr, INT16U data)
{
R_IOD_DIR=0xFFFF; //set IO output enable
R_IOD_O_DATA=data; //Put data before setting address
nop();
R_IOC_O_DATA=IO_READ_H|IO_WRITE_H|addr; //setting address & write
nop();
}
INT16U read_cmd(INT16U addr)
{
INT16U data=0;
R_IOD_DIR=0x0000; //set IO input enable
R_IOC_O_DATA=IO_READ_L|IO_WRITE_H|addr;
nop();
data=R_IOD_I_DATA;
nop();
return data; //return read from ISA bus data
}
/******************************************************************/
Example:Use GPIO simulate 8080 port
void lcm_write (INT8U data, INT8U type)
INT8U data: write data
INT8U type: data type 0:command 1 : display data
INT8U lcm_read_cmd(void):read data from LCM
void wait_lcm_busy(void):wait LCM busy
/******************************************************************/
void lcm_write (INT8U data, INT8U type)
{
R_IOE_O_DATA&=0x0007;
nop();
if(type==0)
R_IOE_O_DATA|=LCM_W|LCM_DIR_O|LCM_ENABLE|LCM_CMD|data;
else
R_IOE_O_DATA|=LCM_W|LCM_DIR_O|LCM_ENABLE|data;
nop();
R_IOE_O_DATA&=~LCM_ENABLE;
}
INT8U lcm_read_cmd(void)
{
INT16U buf=0;
INT8U data=0;
R_IOE_O_DATA|=LCM_R|LCM_DIR_I|LCM_ENABLE|LCM_CMD;
nop();
R_IOE_O_DATA&=~LCM_ENABLE;
buf=R_IOE_I_DATA;
data=buf>>8;
return data;
}
void wait_lcm_busy(void) //wait LCM busy
{
INT8U flag=0;
while(1){
flag=lcm_read_cmd();
if((flag&0x80)==0)
break;
}
}
/*******************************************************************/
Application:write 8 bits data on AT24C04
char id :slave ID ( In this example slave ID use 0xA0 )
INT16U addr:write data address (2 BYTES address)
char data:write data value
/*******************************************************************/
void I2C_write_1byte(char id, INT16U addr, char data)
{
I2C_init(); //I/O set
I2C_start(); //star
I2C_send_8bit(id); //send id
I2C_send_8bit(addr>>8); //send address high byte
I2C_send_8bit(addr); //send address low byte
I2C_send_8bit(data); // send data
I2C_stop(); //finish
delav_msec_wait(5); // write cycle 5ms
}
/*******************************************************************/
Application:Display ”Hello word!!” on LCM(WH1602C)
/*******************************************************************/
void lcm_display(void){
INT8U string_arry[12]={“Hello word!!” };
INT8U flag=0;
// LCM init
lcm_write_cmd(0x38);//0x3c//LCM init
drv_msec_wait(1);
lcm_write_cmd(0x0F);//set display
drv_msec_wait(1);
lcm_write_cmd(0x01);//display clear
drv_msec_wait(2);
lcm_write_cmd(0x06);//show word mode setting
//display string ” Hello word!! ”
lcm_write_cmd(0x80) //display line 1;
wait_lcm_busy(); // wait LCM busy
for(i=0,j=0;i<12;i++) //write string
{
lcm_write_data( string_arry[i] );
wait_lcm_busy(); // wait LCM busy
}
}