So I have an spi_main.c file which needs an spi.c file. The spi.c file will have the read and write functions and some delay functions in it. I have some of the functions written in the spi.c file but I stopped because I'm not sure if I'm wasting my time by doing it completely wrong. Maybe someone can give it a look?
The idea is just to a basic read and write between the microcontroller and the memory module.
Code:
//spi_main.c
#include <stdio.h>
#include <pic.h>
#include <htc.h>
#include "spi.c"
__CONFIG(INTIO & WDTDIS & PWRTEN & BORDIS);
void init (void);
/*******************************************/
// MAIN - SPI COMMUNICATION
/*******************************************/
#define Databyte 0x05
#define Datapos 0x03
void main (void)
{
char temp;
init();
short_delay(10000);
while(1)
{
// Write and read back...
write_SPI(Databyte,Datapos); // Write one byte
delay(100000);
temp = read_SPI(Datapos); // Read one byte
// Is our SPI interface working...?
if(temp == Databyte)
flash_led_ok();
else
flash_led_error();
// Job done, Relax some clock cycles...
while(1);
}
}
/*******************************************/
// INIT - RUN AT STARTUP
/*******************************************/
void init (void)
{
ANSEL=0;
// Port directions
TRISC0 = 0; //-> Chip select (CS)
TRISC1 = 1; //-> Serial input to microcontroller
TRISC2 = 0; //-> Serial clock (SCK)
TRISC3 = 0; //-> Serial output from microcontroller
// Init values
RC0 = 1; // CS high
RC2 = 0; // SCK low
}
Code:
// spi.c
/********************************/
// Prototypes
/********************************/
write_SPI(char,int);
read_SPI(int);
void short_delay(int);
void delay(int);
void long_delay(void);
void flash_led_ok(void);
void flash_led_error(void);
/********************************/
// Functions
/********************************/
write_SPI(char Databyte, int Datapos)
{
char instr,i;
RC0 = 0;
instr = 0x03;
for()i=0;i=<7;i++)
{
RC3 = (instr>>(7-i)) & 0x01;
toggle_clk();
}
}
read_SPI(int Datapos)
{
}
/*******************************************/
// short delay
/*******************************************/
void short_delay(int)
{
unsigned int del;
for(del=0;del<2000;del++){;}
}
void delay(int)
{
unsigned int del;
for(del=0;del<2000;del++){;}
}
/*******************************************/
// long delay, used at init...
/*******************************************/
void long_delay(void)
{
unsigned int j;
for(j=0;j<60000;j++){;}
}
void flash_led_ok(void)
{
}
void flash_led_error(void)
{
}
The idea is just to a basic read and write between the microcontroller and the memory module.
Last edited: