This is my first question as I am new to coding.
Well, I am trying to use a MCP digipot in SPI. As I am using a PIC16, writing in C, I learned that the XC8 library couldn't be included in a PIC16 project... So I thought I would learn a lot doing it by bit banging, understanding what I do...
But...Doesn't seem to work ! So I have made a LED version with some delay in order to see what was happening.
I have two problems: First, the "command" data doesn't seem to be shifted. I can only see a flash for the first bit then it turns out. The "clock" led works fine, flashing 8 times.
Also, I don't understand why my infinite loop isn't infinite. I get a few cycles then the main function is called back and so on. It has something to do with my delays as it changes the number of cycles when I change the delay.
Here is my code:
#include <stdio.h>
#include <pic16f18875.h>
#define temoin LATAbits.LATA0
#define CS LATAbits.LATA1
#define CLK LATAbits.LATA2
#define DAT LATAbits.LATA3
#define MASK = 0x80 //isolate the MSB
unsigned char commande = 0b10011000; //led sequence to be observed (command)
void delay(void)
{ int f, g;
for(f=0;f<1000;f++)
{
for(g=0;g<1;g++)
{ } } } //simple delay
void main()
{
int i;
//init
TRISA=0x00; //porta as output
LATA = 0x00; //init all bits to 0
CS=0; //enable writing
//send command
for (i=0; i<8; i++) //browse through the command byte
{
if (commande & MASK) //read msb and compare
DAT = 1;
else
DAT = 0;
CLK=1; //clock signal
delay();
CLK=0;
delay();
commande <<=1; //left shift, read next bit
}
CS = 1;
while (1) //just a heartbeat
{
temoin = 1;
delay();
temoin = 0;
delay();
}
}
I have really tried a lot to find any info on the web but none worked fine for me. I might just lack a special C language knowledge, but I couldn't tell what.
Thanks in advance !
Cheers