ok mercy...

Status
Not open for further replies.

keny

New Member
I have two MCU's (the parts are here and this is what I have to work with)

PIC18F4550:
USB connection to host
Several connections to I/O

PIC18F2550
Scanner Algorithm for LED matrix

The computer is to send packets (containing an array of numerical values) to the 4550 through USB, get forwarded along to the 2550, where the data is interpreted and the matrix is thereby driven. It will comprise two 4x5 matrices, currently one.

the plan, get the two chips to talk together by USART, SPI or I2C. I'm very close with USART, not with the others...

I am wondering if you might have some code I could use or convert or something that would help me along.

I used this tutorial to get the USB capability working...
Open Source Framework for USB Generic HID devices based on the PIC18F and Windows - WFFwiki

Best Regards,
Keny
 
Ok heres a picture of my scope reading of this project:

**broken link removed**
(I'd be ecstatic if someone could explain the scope reading to me)


The Bottom Line is the transmit pin from the 4550 and the top line is the transmit pin from the 2550. This is supposed to somehow be the letters "a" and "b". The 4550 is generating the output.

In the 2550, I have the following code:


while(1){
input = getch();
putchar(input);

for the life of me, the dang proof is in the scope reading, how do I get at the variables so I can do something with them?
i tried if input is equal to (blah blah blah) but i am just reaching around in the dark here.

so I'm just about there. All I need to do is figure out how to get at the incoming binary.

input?
 
Last edited:
If on one chip you send "a" and then "b" repeatedly but only echo back the "a" then you will be able to see the difference.

So, something like,
Code:
    while(1){
        input=getch();
        if(input=='a')
            putchar(input);
    }

Edit, are you sure getch waits until a byte is ready? You may need to use something like if(datardy).

BTW, to keep the formating of code put
Code:
 before it and
after it.

Mike.
 
Last edited:
if I do that the output of the 2550 is high all the time. no packets...


weird... sometimes it doesn't respond at all. more code:

same config word for both MCU's
Code:
#include <stdio.h>
#include <htc.h>
#include "usart.h"

// PIC 18F4550 fuse configuration:
// Config word 1 (Oscillator configuration)
// 20Mhz crystal input scaled to 48Mhz and configured for USB operation
__CONFIG(1, USBPLL & IESODIS & FCMDIS & HSPLL & CPUDIV1 & PLLDIV5);
// Config word 2
__CONFIG(2, VREGEN & PWRTDIS & BOREN & BORV20 & WDTDIS & WDTPS32K);
// Config word 3
__CONFIG(3, PBDIGITAL & LPT1DIS & MCLREN);
// Config word 4
__CONFIG(4, XINSTDIS & STVREN & LVPDIS & ICPORTDIS & DEBUGDIS);

void main(void){
	unsigned char input;

	TRISA = 0b00000000;
	SPEN = 1;
	TRISC = 1;

...rest of program

include files...

Usart.h
Code:
#ifndef _SERIAL_H_
#define _SERIAL_H_

#define BAUD 9600      
#define FOSC 4000000L
#define NINE 0     /* Use 9bit communication? FALSE=8bit */

#define DIVIDER ((int)(FOSC/(16UL * BAUD) -1))
#define HIGH_SPEED 1

#if NINE == 1
#define NINE_BITS 0x40
#else
#define NINE_BITS 0
#endif

#if HIGH_SPEED == 1
#define SPEED 0x4
#else
#define SPEED 0
#endif

#if defined(_16F87) || defined(_16F88)
	#define RX_PIN TRISB2
	#define TX_PIN TRISB5
#else
	#define RX_PIN TRISC7
	#define TX_PIN TRISC6
#endif

/* Serial initialization */
#define init_comms()\
	RX_PIN = 1;	\
	TX_PIN = 1;		  \
	SPBRG = DIVIDER;     	\
	RCSTA = (NINE_BITS|0x90);	\
	TXSTA = (SPEED|NINE_BITS|0x20)

void putch(unsigned char);
unsigned char getch(void);
unsigned char getche(void);

#endif

usart.c

Code:
#include <htc.h>
#include <stdio.h>
#include "usart.h"

void 
putch(unsigned char byte) 
{
	/* output one byte */
	while(!TXIF)	/* set when register is empty */
		continue;
	TXREG = byte;
}

unsigned char 
getch() {
	/* retrieve one byte */

// having issues here... RCIF appears to not exist and therre doesn't appear to be anything to make it exist, probably mplabsim app not geting input

	while(!RCIF)	/* set when register is not empty */
	{				
		continue; // mplab sim seems to hang here
	}
	return RCREG;	
}

unsigned char
getche(void)
{
	unsigned char c;
	putch(c = getch());
	return c;
}
 
Last edited:
That should be the receiving end, why isn't the other end sending?

Which compiler are you using?

Mike.
 
It's the same program on both sides, the sending end just pipes the letters a and b into printf:
Code:
				input = "a";
				printf(input);	// echo it back
				input = "b";
				printf(input);	// echo it back

I am using HI Tech C - Universal with MPLAB
 
only other thought is that i only have rx and tx hooked up but I dont think you need a clock connection right? I wouldn't be against changing to spi or I2c (but im two 10 hour days into this and banging my head against a wall) I'll get it, and feel great afterwards sooner or later...
 


How can it be the same program on each end if one end does the above and the other end does as I posted above!!

Mike.
 
I think I cracked it. I got rid of all the stdio.h links and deleted anything that didnt matter. then i READ THE MANUAL again, and wrote the acronym breakdown in the comments below. Everything just looked so foreign to me when I started that I just didn't know what to look for. In the manual they say do this to this bit and that to that bit then they put <7> next to it and a separate acronym which is rather confusing. I managed to put the pieces together with code snippets and raw staying power to keep at it. Last week I never programmed an MCU in my life.. αβΩ

this isnt without some weirdness though. I can only currently recognize 4 bit numbers. I tried 8bit numbers but cant seem to identify them. so i have to send 8 bits is two packets or something, although I haven't tried to recognize 5, 6, or 7 bit binary yet...

cheers guys thanks for the steering.

keny

here is the sending program

Code:
#include <htc.h>
#include "usart.h"

// PIC 18F4550 fuse configuration:
// Config word 1 (Oscillator configuration)
// 20Mhz crystal input scaled to 48Mhz and configured for USB operation

// USBPLL clk src from 96MHz PLL/2 
// IESODIS Internal External Switch Over Mode
// FCMDIS Fail-Safe Clock Monitor Enable
// HSPLL Oscillator: HS: HS+PLL, USB-HS 
// CPUDIV1 CPU Sys CLK Select: no divide 
// OSC Select: PLLDIV5 div by 5 {20MHz input}

__CONFIG(1, USBPLL & IESODIS & FCMDIS & HSPLL & CPUDIV1 & PLLDIV5);

// Config word 2
// VREGEN USB Voltage Regulator
// PWRTDIS Power Up Timer
// BOREN Brown Out Detect: Enabled in hardware, SBOREN disabled 
// BORV2O Brown Out Voltage: 2.0V 
// WDTDIS Watchdog Timer
// WDTPS32K Watchdog Postscaler: 1:32768 

__CONFIG(2, VREGEN & PWRTDIS & BOREN & BORV20 & WDTDIS & WDTPS32K);

// Config word 3
// PBDIGITAL PortB A/D Enable: PORTB<4:0> configured as digital I/O on RESET 
// LPT1DIS Low Power Timer1 Osc enable: Timer 1 not in low power configuration
// Master Clear Enable: MCLREN MCLR Enabled,RE3 Disabled 

__CONFIG(3, PBDIGITAL & LPT1DIS & MCLREN);

// Config word 4
// XINSTDIS Extended CPU Enable: Disable extended instruction set (Legacy mode)
// STVREN Stack Overflow Reset
// LVPDIS Low Voltage Program
// ICPORTDIS Dedicated In-Circuit Port {ICD/ICSP}
// DEBUGDIS Background Debug
 
__CONFIG(4, XINSTDIS & STVREN & LVPDIS & ICPORTDIS & DEBUGDIS);

void main(void){
	unsigned char eightBit;

	TRISA = 0b00000000;

// Enable the asyncchronous serial port.
	SYNC = 0 ;				// Asynchronous mode	
	SPEN = 1;				// Serial port enable.
	TRISC6 = 0;
	TRISC7 = 1;
//Configure for Transmitter mode.	
	TXEN  = 1; 				// Transmit enable 
//Configure for Receiver mode
	CREN = 1;				// Enable the reception 
//Interrupt
	RCIF = 0;
	RCIE = 1;				// Reception Interrupt Enable
	GIE = 1;				// Global Interrupt Enable
	PEIE = 1;				// Perapheral Interrupt Enable

	INTCON=0;	// purpose of disabling the interrupts.
	init_comms();	// set up the USART - settings defined in usart.h


	while(1){
		RA3 = 0;


	// TXREG - Read/Write Transmit buffer Register 
	//		 - loaded with data by software
	//		 - empties once transfer to shift register occurs
	//		 - once empty TXIF Flag is set
	// TXIF	 - not alterable by sofware
	// 		 - if polling wait a tid after TXREG
	// TXIE  - interrupt clearing mechanism
	// TRMT  - shows shift register status. read only 
	//		 - poll to determine if Shift register is empty


	// SYNC  - Enable Asynchronous: 0;
	// SPEN  - Serial port setting bit: 1;
	// RCIE  - Interrupt enable
	// RX9   - 9 bit reception
	// CREN  - Enables reception
	// RCIE  - Flag to generate RCIF interrupts
	// RCIF  - Reception flag. Indicates complete reception
	// RCSTA - 9th bit location. error checking
	// RCREG - Reads 8 bit data received
	// CREN  - Clear error, clearing enable bit
	// GIE and PEIE should be set to use interrupts

		//	eightBit = getch();

		    eightBit = 0010;
			putch(eightBit);
		    eightBit = 0001;
			putch(eightBit);
			RA3 = 1;
			//	}

	}

and here is the receiving program

Code:
void main(void){
	unsigned char eightBit;

	TRISA = 0b00000000;
//	SPEN = 1;
//	TRISC = 1;
// Enable the asyncchronous serial port.
	SYNC = 0 ;				// Asynchronous mode	
	SPEN = 1;				// Serial port enable.
	TRISC6 = 0;
	TRISC7 = 1;
//Configure for Transmitter mode.	
	TXEN  = 1; 				// Transmit enable 
//Configure for Receiver mode
	CREN = 1;				// Enable the reception 
//Interrupt
	RCIF = 0;
	RCIE = 1;				// Reception Interrupt Enable
	GIE = 1;				// Global Interrupt Enable
	PEIE = 1;				// Perapheral Interrupt Enable

	INTCON=0;	// purpose of disabling the interrupts.

	init_comms();	// set up the USART - settings defined in usart.h

	while(1){
		RA3 = 0;

			eightBit = getch();

			if (eightBit == 0010)
			{
				putch(eightBit);
				RA4 = 1;

			}
			if (eightBit == 0001)
			{

				putch(eightBit);
				RA3 = 1;
				RA5 = 1;
			}
	}
}

usart.c
Code:
#include <htc.h>
#include "usart.h"

void 
putch(unsigned char byte) 
{
	/* output one byte */
	while(!TXIF)	/* set when register is empty */
		continue;
	TXREG = byte;
}

unsigned char 
getch() {
	/* retrieve one byte */


	while(!RCIF)	/* set when register is not empty */
	{				
		continue;
	}
	return RCREG;	
}
 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…