PIC Beginner - Page 1
Forum section: electronics / beginners

author: MrAureliusR
author_rank: Unknown
timestamp: June 02, 2014, 01:39:56 am
content: Hey everyone. I've been using AVR and TI micros for a while now, but for various reasons I need to try and build a (fairly) simple project using a PIC. I have several PICs at hand, and I've chosen the PIC18F4685 as it seems to have everything I need for this project.I'm not at all familiar with the PIC registers (I'm programming in C). I've figured out the basics so far, like the data direction registers are TRISx; the inputs are read from PORTx; and the outputs go to LATx.So I think I can handle GPIO. My question is regarding SPI. I need to communicate with a MAX7219, which uses SPI. I know the SPI protocol well, and I have used this chip before successfully. I just need to translate that into PIC C.So, my two most burning questions are: how do I set the config fuses inside the PIC to use the internal oscillator, with all the standard options? (ie, serial programming enabled, watchdog off, brownout detect off, lock bits off, and so forth). I just need to understand the basic fuse configuration procedure and I can take it from there. I've seen various approaches online, some using #pragma config, some using __CONFIG(), not sure which is supposed to be used with the 18F4685, and even what the options are. The fuses shown in the MiniPro programmer software are overwhelming (at least compared to AVR) and I'm not sure what each do. I'm slowly sifting through the datasheet for the 18F4685, and it definitely has a lot of good info, but it doesn't have any C programming examples!The other is how to get SPI up and running properly. Is there an spi.h somewhere? Or do I just control it through registers, like in my code below?Also, IthinkI've figured out how to initialize the SPI as master on the 18F4685... this is what I have so farCode:/** File: main.c* Author: Max** Created on June 1, 2014, 8:55 PM*/#include <xc.h>#include <stdio.h>#include <stdlib.h>#include <pic18f4685.h>// need to figure out how to configure the fuses// within the code...// we want INTIO2 for clock#pragma/** the SPI module has four registers,* SSPCON1, SSPSTAT, SSPBUF, SSPSR* Control, Status, I/O Buffer, not accessible**/void setupSPI(void){SSPSTAT = 0x00; // all bits need to be 0 hereSSPCON1 = 0x21; // set up SPI as master, clock is Fosc/16}void setupMAX(void){// MAX7219 init code to go here}uint8_t getBuses(void){// this function will read the address and data buses, and send the results to the MAX7219 via SPIuint8_t addrH = 0x00;uint8_t addrL = 0x00;uint8_t data = 0x00;uint8_t addrH_prev;uint8_t addrL_prev;uint8_t data_prev;addrH = PORTB; // still not quite sure how I'm going to juggle the input of the buses around the pins I have available.addrL = PORTD; // the problem is that the SPI is right in the middle of port C, and /SS is in the middle of port A. I guess I'll have to mask those bits, and do some shifting and or'ing.}void main(void) {// set up portsTRISB = 0xFF; // PORTB all inputsTRISA = 0xDF; // PORTA RA5 is output, as /SSTRISC = 0xD7; // PORTC SDO, SCK outputsTRISD = 0xFF; // PORTD all inputssetupSPI();while(1){}}I am a pretty quick learner, I just need to get it sending SPI bytes and I can handle the rest (I think). I'm using MPLAX X IDE v2.00 with the XC8 compiler. I've got an imitation PicKit 3 (which has worked fine the couple times I've had to burn .hex files to PICs). I also have my MiniPro programmer, so I'm not worried about programming the thing. Does the PicKit have debugging built in?Any and all help is greatly, and I meangreatlyappreciated!!EDIT: I just tried to build, and it seems the compiler doesn't know what to do with uint8_t as a type... not sure what to do about that? Just use int? Or does the compiler assume that int is 16 bits? I read somewhere that the PIC compilers treat different types in different ways.... how confusing!

author: David_AVD
author_rank: Super Contributor
timestamp: June 02, 2014, 02:23:23 am
content: Quote from: MrAureliusR on June 02, 2014, 01:39:56 amuinit8_t is valid in xc8 (MPLABX).

author: mariush
author_rank: Super Contributor
timestamp: June 02, 2014, 02:51:31 am
content: Add#include <stdint.h>after #include <xc.h>The uint8_t is defined in stdint.h in XC8 (see <install path>\xc8\v1.31\docs\MPLAB_XC8_C_Compiler_User_Guide.pdf page 21)ps. alternatively, do a search and replace and replace uint8_t with "unsigned char" (though char is the same as unsigned char in xc8) ... in xc8 , char is an unsigned byte, just what uint8_t is supposed to be.<install path>\xc8\v1.31\include\stdint.h:Quote

author: MrAureliusR
author_rank: Unknown
timestamp: June 02, 2014, 03:57:02 am
content: Quote from: mariush on June 02, 2014, 02:51:31 amThanks for the tip mariush! That worked.Anyone have any info on the config bits? I'm still looking for something relating specifically to the 4685.

author: mariush
author_rank: Super Contributor
timestamp: June 02, 2014, 04:14:30 am
content: Datasheet for that PIC has a chapter where all those config bits are explained.Also, once you select the microcontroller in your project properties in mplabx , you can go to window > pic memory views > configuration bits ...select what you want then click generate source code to output . copy that into your project after the #include lines.

author: MrAureliusR
author_rank: Unknown
timestamp: June 02, 2014, 07:13:09 am
content: Awesome, thanks for the help.It seems my switch to PIC was a good one. I have it sending the SPI data perfectly. Still some kinks to iron out, but it's working a hell of a lot better than the AVR was.Switching to PIC was much easier than I was expecting. I guess my earlier statement that C is C is C turned out to be true. Everything has a sort of familiarity to it, even though all the terms are different, things are still the same.Here's hoping I can get everything working when I get up tomorrow!