Thursday, July 21, 2016

(Firmware) SPI using GPIO : The code

#ifndef __SPI__H__
#define __SPI__H__

extern unsigned char time_y = 8;

void SPI_Master_GPIO_Init(void)
{
PC_DDR|=(1<<6 mosi="" nbsp="" span="">
PC_CR1|=(1<<6 span="">
PC_CR2&=~(1<<6 span="">

PC_DDR|=(1<<5 nbsp="" span="">
PC_CR1|=(1<<5 span="">
PC_CR2&=~(1<<5 span="">

PA_DDR|=(1<<3 nbsp="" out="" span="">
PA_CR1|=(1<<3 span="">
PA_CR2&=~(1<<3 span="">

PC_DDR&=~(1<<7 in="" miso="" nbsp="" span="">
PC_CR1|=(1<<7 span="">
PC_CR2&=~(1<<7 span="">
}

void CSN_low (void)
{
PA_ODR &= ~(1<<3 span="">
}

void CSN_high (void)
{
PA_ODR |= (1<<3 span="">
}


void SPI_Slave_GPIO_Init(void)
{
PC_DDR&=~(1<<6 span="">
PC_CR1|=(1<<6 span="">
PC_CR2&=~(1<<6 span="">

PC_DDR&=~(1<<5 span="">
PC_CR1|=(1<<5 span="">
PC_CR2&=~(1<<5 span="">

PA_DDR&=~(1<<3 span="">
PA_CR1|=(1<<3 span="">
PA_CR2&=~(1<<3 span="">

PC_DDR|=(1<<7 span="">
PC_CR1|=(1<<7 span="">
PC_CR2&=~(1<<7 span="">
}




void send_char (unsigned int temp)
{
unsigned char j = 0, i = 0;

for(i=0;i<8 i="" span="">
{

j = 7-i;
PC_ODR &= ~(1<<5 span="">

if((temp>>j) & 0x01)
PC_ODR |= (1<<6 span="">
else
PC_ODR &= ~(1<<6 span="">

time_y = 8;
 while(time_y--);
PC_ODR |= (1<<5 span="">
 time_y = 8;
 while(time_y--);
PC_ODR &= ~(1<<5 span="">
}

}




unsigned char get_char (void)
{
unsigned char j = 0, i = 0;

for(i=0;i<8 i="" span="">
{
unsigned char k = 7-i;

PC_ODR &= ~(1<<5 span="">



time_y = 8;
 while(time_y--);
PC_ODR |= (1<<5 span="">

if(PC_IDR & (1<<7 span="">
{
j |= (1<
 }
else
time_y = 8;
 while(time_y--);
PC_ODR &= ~(1<<5 span="">

}
return j;

}

void write_n_byte (unsigned char add,unsigned char no_of_byte,unsigned char *string_data)
{
unsigned char i = 0;
add |=0x20;
send_char(add);
for (i = 0; i < no_of_byte ; i++)
{
send_char(*string_data+i);
}
}








#endif

No comments:

Post a Comment