Society of Robots - Robot Forum
Electronics => Electronics => Topic started by: hazzer123 on April 17, 2007, 01:33:06 PM
-
Hi
I am trying to write some code, in assembly, to communcate serially between a PIC17F628a and HyperTerminal.
The PIC has an inbuilt USART, but i was just wondering, will i have to use something like a MAX232 to lower the voltages coming from my computer, + and - 12V, in order for the PIC to be safe.
Another thing - the signals coming out of the PC are reversed, a high signal is -12V and vice versa. Will this need to be inverted for the USART in the PIC to receive it or do USARTs automatically do this?
Thankyou
-
The MAX232 will automatically handle inverting the data, and translating the voltages appropriately...
- Jon
-
#include <pic.h>
#include <stdio.h>
#include "usart.h"
/********************************************************************************************************************************
* Author: Ian Robinson *
* Date Created: December 2006 *
* Project: EG3001 Project1 : Ultrasonic 3D position location *
* Project Synopsis: A project to show ultrasonic 3D position location over an RS-232 link using the principle of trilateration *
********************************************************************************************************************************/
__CONFIG(BORDIS & UNPROTECT & MCLRDIS & PWRTEN & WDTDIS & INTIO);
#define PORTBIT(adr, bit) ((unsigned)(&adr)*8+(bit))
static bit LED0 @ PORTBIT(PORTB, 0);
// B1 used for RX
// B2 used for TX
static bit LED3 @ PORTBIT(PORTB, 3);
static bit RX1 @ PORTBIT(PORTB, 4); // RX1 (RB4) TOP RIGHT
static bit RX2 @ PORTBIT(PORTB, 5); // RX2 (RB5) BOTTOM RIGHT
static bit RX3 @ PORTBIT(PORTB, 6); // RX3 (RB6) TOP LEFT (SET AS INPUTS WHEN TIMER 1 FUNCTIONS)
static bit RX4 @ PORTBIT(PORTB, 7); // RX4 (RB7) BOTTOM LEFT (SET AS INPUTS WHEN TIMER 1 FUNCTIONS)
//TRISB4 =1; //Put RB4 into input mode
//TRISB5 =1; //Put RB5 into input mode
//TRISB6 =1; //Put RB6 into input mode
//TRISB7 =1; //Put RB7 into input mode
/***********************************************Variable Declaration************************************************************/
unsigned int i,j; //for loop pause
unsigned int timerReading;
unsigned int timerReadingRX1;
unsigned int timerReadingRX2;
unsigned int compare;
/***********************************************Function Declaration************************************************************/
void initialRecieve(void);
void check1(void);
void check2(void);
/***********************************************Main Function*******************************************************************/
void main(void){
unsigned char input;
INTCON=0; // purpose of disabling the interrupts.
init_comms(); // set up the USART - settings defined in usart.h
TRISB7 = 0;
T1CON = 0b00001100; // Timer 1 control register set-up
TMR1H = 0; // Clear high bit
TMR1L = 0; // Clear low bit
T1OSCEN = 0; // Disable Built-in oscillator between RB6 and RB7
GIE = 1; // enable global interrupts
PEIE = 1; // enable peripheral interrupts
TMR1IF = 0; // clear Timer 1 interrupt flag
TMR1ON = 1; // start Timer 1
TMR1IE = 1; // enable Timer 1 interrupt
timerReading = 0;
compare = 0;
/***********************************************While Loop*********************************************************************/
while(1){
// initialRecieve();
// TMR1ON = 0;
// timerReading = (TMR1H << 8) + TMR1L;
// TMR1ON = 1;
// printf("%10u\t",timerReading);
if(RB7 == 0)
{
//printf("%10u\t",timerReading);
printf("RB7");
}
}
}
/***********************************************End of Main and While Loop**************************************************************/
/***********************************************Initial Recieve Function****************************************************************/
void initialRecieve(void){
while(1)
{
if(RX1 == 0)
{
timerReadingRX1 = 0;
TMR1ON = 1;
check2();
}
else if(RX2 == 0)
{
timerReadingRX2 = 0;
TMR1ON = 1;
check1();
}
}
}
/***********************************************Check for 1 Function****************************************************************/
void check1(void){
while(RX1 != 0)
{
if(RX1 == 0)
{
TMR1ON = 0;
timerReadingRX1 = (TMR1H << 8) + TMR1L;
printf("%10u\nTX1 is",timerReading, "TX2 is 0");
timerReading = 0;
}
}
}
/***********************************************Check for 2 Function****************************************************************/
void check2(void){
while(RX2 != 0)
{
if(RX2 == 0)
{
TMR1ON = 0;
timerReadingRX2 = (TMR1H << 8) + TMR1L;
printf("%10u\nTX2",timerReading, "TX1 is 0");
timerReading = 0;
}
}
}
/***********************************************Calculate Distances****************************************************************/
void calculate(void){
}
/***********************************************Delay Function****************************************************************/
void delay()
{
for(i=0; i<4000; i++)
{
for(j=0; j<30; j++)
{
}
}
}
/***********************************************Interrupt (ISR) Routine*********************************************************/
void interrupt tmr1int(void)
{
if(TMR1IF)
{
TMR1IF=0;
timerReading = 0;
}
}
/**************************************************END OF CODE****************************************************************/
The printf statement sends stuff to hyperterminal.
Take what you like from the code. I was using the same PIC.
Good luck :)