CYRAX
Newbie level 2
- Joined
- Feb 16, 2014
- Messages
- 2
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Activity points
- 106
what i have is a 16F877 & MAX3100 (Saving PIC's USART For Something Down The Road) I'm trying to send a simple "Hello" message to my rs232 port on my PC which I have RealTerm monitoring the port but having no luck getting it to work... this being my first project ever working with pic's I thought it would be a little easier then this lol so time to swallow my pride and ask for some help. When I power up the chip the led does flash also connected with pickit3 for debugging and ive followed the code using the step function and it seems to be pushing the letters into SSPBUF but im not getting anything in RealTerm as of now I have no probes or scopes so no way of knowing if the spi is functioning properly to know if its making it to the 3100
here's what I've got so far *sorry for all the code comments like a said im just starting out so trying to remember what everything does
Setup like this pin connections are listed in header
PIC16F877 --> MAX3100 --> MAX3232CPE --> PC
(16mhz) (3.6864mhz)
MPLABX V2.0 & HI-TECH V9.83
main.h
main.c
spi.h
spi.c
project is also included, Thanks for any help to get this working.
here's what I've got so far *sorry for all the code comments like a said im just starting out so trying to remember what everything does
Setup like this pin connections are listed in header
PIC16F877 --> MAX3100 --> MAX3232CPE --> PC
(16mhz) (3.6864mhz)
MPLABX V2.0 & HI-TECH V9.83
main.h
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 /* * File: main.h * Author: CYRAX * * Created on February 16, 2014, 11:42 AM */ #ifndef MAIN_H #define MAIN_H #define PIC_CLK 16000000 /* Clock Frequency in Hz */ // Function prototypes extern void MAX3100_init(void); extern void SendString(unsigned char *str); extern void delay( char millisec ); extern void putst(register const char *str); // Manual SPI control for MAX3100 // #define MAX3100_IRQ RB2 //Set As Input (PIC PIN35 -> MAX3100 PIN5)/* IRQ */ #define MAX3100_CS RB4 //Set As Output (PIC PIN37 -> MAX3100 PIN4)/* Chip Select */ #define MAX3100_SPI_CLK RC3 //Set As Output (PIC PIN18 -> MAX3100 PIN3)/* SPI Clock */ #define MAX3100_SPI_IN RC4 //Set As Input (PIC PIN23 -> MAX3100 PIN2)/* SPI Data In */ #define MAX3100_SPI_OUT RC5 //Set as Output (PIC PIN24 -> MAX3100 PIN1)/* SPI Data Out */ /* Port A used for analog inputs (ex. LEDS) */ #define PORTA_INIT 0b00000000 /* PORTA init data */ #define TRISA_INIT 0b00000000 /* SETUP PORT A I/O*/ /* ==== ADCON0 REGISTER ====*/ #define ADCON0_INIT 0b11000001 /* A/D Control Register0 - controls the operation of the A/D module. RC osc, CH0 selected *//* * View The PIC16F877 DataSheet Under "ADCON0 REGISTER" ==== bit 0 ADON: A/D On bit ==== 1 = A/D converter module is operating 0 = A/D converter module is shut-off and consumes no operating current ================================= ==== bit 1 Unimplemented: Read as '0' ==== ========================================== ==== bit 2 GO/DONE: A/D Conversion Status bit ==== If ADON = 1: 1 = A/D conversion in progress (setting this bit starts the A/D conversion) 0 = A/D conversion not in progress (this bit is automatically cleared by hardware when the A/D conversion is complete) ========================================== ==== bit 5-3 CHS2:CHS0: Analog Channel Select bits === 000 = channel 0, (RA0/AN0) 001 = channel 1, (RA1/AN1) 010 = channel 2, (RA2/AN2) 011 = channel 3, (RA3/AN3) 100 = channel 4, (RA5/AN4) 101 = channel 5, (RE0/AN5) 110 = channel 6, (RE1/AN6) 111 = channel 7, (RE2/AN7) ========================================== ==== bit 7-6 ADCS1:ADCS0: A/D Conversion Clock Select bits ==== 00 = FOSC/2 01 = FOSC/8 10 = FOSC/32 11 = FRC (clock derived from the internal A/D module RC oscillator) *END OF ADCON0 REGISTER =========================================== */ /* ==== ADCON1 REGISTER ====*/ #define ADCON1_INIT 0b10001110 /* A/D Control Register1 - configures the functions of the port pinsright justified 1 AD ch, int. ref *//* * View The PIC16F877 DataSheet Under "ADCON1 REGISTER" ==== bit 7 ADFM: A/D Result Format Select bit ==== 1 = Right justified. 6 Most Significant bits of ADRESH are read as ?0?. 0 = Left justified. 6 Least Significant bits of ADRESL are read as ?0?. ==== bit 6-4 Unimplemented: Read as '0' ==== ==== bit 3-0 PCFG3:PCFG0: A/D Port Configuration Control bits: ==== (bit) |AN7|AN6|AN5|AN4| AN3 |AN2|AN1|AN0|VREF+|VREF-|CHAN/REF (0011)| D | D | D | A |VREF+| A | A | A | RA3 | VSS | 4/1 *A = Analog input D = Digital I/O *END OF ADCON1 REGISTER =========================================== */ #define TXLED_ON RA1 /* Set As Output (PIC PIN3 -> LED */ /* Port B signals * RB2 <-- Input * RB4 --> Output */ #define PORTB_INIT 0b00000000 /* PORTB init data */ #define TRISB_INIT 0b00000100 /* SETUP PORT A I/O*/ /* Port C signals * RC3 --> Output * RC4 <-- Input * RC5 --> Output */ /* RC6 - RC7 are reserved for serial IO */ #define PORTC_INIT 0b00000000 /* PORTC init data */ #define TRISC_INIT 0b00010000 #endif /* MAIN_H */
main.c
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 /* * File: main.c * Author: CYRAX * */ #define _LEGACY_HEADERS /* -- INCLUDE -- (<> = Look in compiler dir, "" = Look in project dir)*/ #include <stdio.h> #include <stdlib.h> #include <htc.h> #include "main.h" #define _XTAL_FREQ PIC_CLK //Clock Frequency in Hz /* COMPILER CONFIG */ __CONFIG(UNPROTECT & WDTDIS & BOREN & HS & DEBUGEN & WRTDIS & DUNPROT & PWRTEN & LVPDIS ); // config bits for V9.83 void main(void) { // CLRWDT(); //HI-TECH function to clear watch dog timer /* Init IO Ports */ // Set up PORTA TRISA = TRISA_INIT; PORTA = PORTA_INIT; ADCON0 = ADCON0_INIT; ADCON1 = ADCON1_INIT; // Set up PORTB TRISB = TRISB_INIT; PORTB = PORTB_INIT; // Set up PORTC TRISC = TRISC_INIT; PORTC = PORTC_INIT; // init SPI controller & MAX3100 Baudrate: 19200 MAX3100_init(); // start main process loop for (;;) { delay( 250 ); SendString("Hello"); delay( 250 ); } } void SendString(unsigned char *str) { TXLED_ON = 1; //LED ON delay(250); putst(str); TXLED_ON = 0; //LED OFF delay(250); } /* Delays a multiple of 1 milliseconds at 16 MHz TMR0 timer */ void delay( char millisec ) { OPTION = 4; /* prescaler divide by 32 */ do { TMR0 = 0; while ( TMR0 < 125); // 125 * 8 = 1000 } while ( -- millisec > 0); }
spi.h
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 /* * File: spi.h * Author: CYRAX * */ #ifndef SPI_H #define SPI_H // Function prototypes extern void MAX3100_init(void); extern void initspi(void); extern unsigned char txrxspi (unsigned char txc); extern unsigned char MAX3100_rxrdy(void); extern unsigned char MAX3100_receive(void); extern void MAX3100_send(unsigned char ch); extern void putst(register const char *str); static void uart_write(unsigned char cbyte, unsigned char dbyte); static unsigned char uart_status(void); /* 3.6864MHz Crystal Baudrates */ #define BR2304 (0 << 0) /* 230400 Baudrate **3.6864MHz Only** | (0000) */ #define BR1152 (1 << 0) /* 115200 Baudrate */ #define BR576 (2 << 0) /* 57600 Baudrate */ #define BR288 (3 << 0) /* 28800 Baudrate */ #define BR144 (4 << 0) /* 14400 Baudrate */ #define BR72 (5 << 0) /* 7200 Baudrate */ #define BR36 (6 << 0) /* 3600 Baudrate */ #define BR18 (7 << 0) /* 1800 Baudrate */ #define BR9 (8 << 0) /* 900 Baudrate */ #define BR384 (9 << 0) /* 38400 Baudrate */ #define BR192 (10 << 0) /* 19200 Baudrate */ #define BR96 (11 << 0) /* 9600 Baudrate */ #define BR48 (12 << 0) /* 4800 Baudrate */ #define BR24 (13 << 0) /* 2400 Baudrate */ #define BR12 (14 << 0) /* 1200 Baudrate */ #define BR6 (15 << 0) /* 600 Baudrate */ #define RDDAT (0 << 6) /* cbyte write: read data */ #define RDCFG (1 << 6) /* cbyte write: read configuration */ #define WRDAT (2 << 6) /* cbyte write: write data */ #define WRCFG (3 << 6) /* cbyte write: write configuration */ #define RXRDY (1 << 7) /* cbyte read: receiver ready */ #define TXRDY (1 << 6) /* cbyte read: transmitter ready */ #define FDS (1 << 5) /* cfg/cbyte: receive FIFO disable */ #define SHDN (1 << 4) /* cfg/cbyte: shutdown */ #define TIE (1 << 3) /* cfg/cbyte: transmit interrupt enable */ #define RIE (1 << 2) /* cfg/cbyte: receive interrupt enable */ #define PIE (1 << 1) /* cfg/cbyte: P bit (ninth data) interrupt enable */ #define FIE (1 << 0) /* cfg/cbyte: activity/framing interrupt enable */ #define IRDA (1 << 7) /* cfg/dbyte: IRDA mode */ #define ST2 (1 << 6) /* cfg/dbyte: two stop bit select */ #define PBE (1 << 5) /* cfg/dbyte: P bit (ninth data) enable */ #define DL7 (1 << 4) /* cfg/dbyte: data length 7 bits */ #define RTSE (1 << 2) /* dat/cbyte write: update only RTS */ #define RAFE (1 << 2) /* dat/cbyte read: rx active / framing error */ #define RTS (1 << 1) /* dat/cbyte: request to send */ #define PBIT (1 << 0) /* dat/cbyte: P bit (ninth data) */ #endif /* SPI_H */
spi.c
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 /* * File: spi.c * Author: CYRAX * * MAX3100 / SPI */ #include <htc.h> /* HiTech General Include File */ #include "main.h" #include "spi.h" #define MODEC RIE /* Receive int enable */ #define MODED BR192 /* Set Baudrate: 19200 bit/s */ #define SPIRATE (0 << SPR1) | (0 << SPR0) /* SPI rate: fast */ #define SPIMODE (1 << SPE) | (1 << MSTR) | SPIRATE /* SPI mode */ // Initialize MAX3100 serial I/O void MAX3100_init(void) { initspi(); /* init 16F877 SPI interface */ uart_write(WRCFG | (MODEC), MODED); /* set UART mode */ while(MAX3100_rxrdy()) /* flush UART receiver */ MAX3100_receive(); } // Init SPI controller void initspi(void) { /*View PIC Datasheet Section "SYNC SERIAL PORT CONTROL REGISTER" */ MAX3100_CS = 1; /* disable MAX3100 chips */ SSPEN = 0; /* SSPEN: Synchronous Serial Port Enable bit * 1 = Enables serial port and configures SCK, SDO, SDI, and SS as the source of the serial port pins * 0 = Disables serial port and configures these pins as I/O port pins if it was enabled */ SSPCON = 0x01; /* Synchronous Serial Port Mode Select bits * 0x00 -> SPI master, Idle low CKP = 0, Fosc/4 * 0x01 -> SPI master, Idle low CKP = 0, Fosc/16 * 0x02 -> SPI master, Idle low, Fosc/64 */ SSPEN = 1; /* SSPEN: Synchronous Serial Port Enable bit * 1 = Enables serial port and configures SCK, SDO, SDI, and SS as the source of the serial port pins * 0 = Disables serial port and configures these pins as I/O port pins if it was enabled */ SSPSTAT = 0x00; /* 0x00 clear status register * 0x40 clear status register, SMP = 0, CKE = 1 */ SSPIF = 0; /* Synchronous Serial Port (SSP) Interrupt Flag - clear irq flag * 0 = No SSP interrupt condition has occurred. * 1 = The SSP interrupt condition has occurred, and must be cleared in * software before returning from the Interrupt Service Routine. The * conditions that will set this bit are: * ? SPI - A transmission/reception has taken place. */ /* Synchronous Serial Port Mode * - 0000 = SPI Master mode, clock = FOSC/4 * - 0001 = SPI Master mode, clock = FOSC/16 * - 0010 = SPI Master mode, clock = FOSC/64 * - 0011 = SPI Master mode, clock = TMR2 output/2 */ /* Slave Mode SS pin Disabled FOSC/16 */ SSPM0 = 1; SSPM1 = 0; SSPM2 = 0; SSPM3 = 0; SSPEN = 1; //SPI port pins enabled CKP = 0; /* 0 = Idle state for clock is a low level * 1 = Idle state for clock is a high level */ } // Write to MAX3100 UART static void uart_write(unsigned char cbyte, unsigned char dbyte) { MAX3100_CS = 0; /* enable MAX3100 chip */ txrxspi(cbyte); txrxspi(dbyte); MAX3100_CS = 1; /* disable MAX3100 chip */ } // Read from MAX3100 UART static void uart_read(unsigned char cbyte, unsigned char *resph, unsigned char *respl) { MAX3100_CS = 0; /* enable MAX3100 chip */ *resph = txrxspi(cbyte); *respl = txrxspi(0); MAX3100_CS = 1; /* disable MAX3100 chip */ } // Transmit and Receive SPI unsigned char txrxspi (unsigned char txc) { SSPBUF = txc; /* start transmission */ while (!BF) ; /* Wait until ready */ if (WCOL || SSPOV) { WCOL = 0; SSPOV = 0; } return (SSPBUF); } // Check for receiver ready unsigned char MAX3100_rxrdy(void) { return (uart_status() & RXRDY) != 0; } // Check for transmitter ready unsigned char MAX3100_txrdy(void) { return (uart_status() & TXRDY) != 0; } // Get MAX3100 UART status static unsigned char uart_status(void) { unsigned char cfgc, cfgd; uart_read(RDCFG, &cfgc, &cfgd); return cfgc; } // Receive a character unsigned char MAX3100_receive(void) { unsigned char sts, ch; while (!MAX3100_rxrdy()) ; uart_read(RDDAT, &sts, &ch); return ch; } void putst(register const char *str) { while((*str)!=0) { MAX3100_send(*str); if (*str==13) MAX3100_send(10); if (*str==10) MAX3100_send(13); str++; } } // Send a character void MAX3100_send(unsigned char ch) { while (!MAX3100_txrdy()) ; uart_write(WRDAT | RTS, ch); }
project is also included, Thanks for any help to get this working.
Attachments
Last edited: