Jump to content


Photo

Eusart Pic16f690 Clock Select Problem


No replies to this topic

#1 drumanart

drumanart

    Regular

  • EstablishedMember
  • Pip
  • 50 posts

Posted 17 June 2009 - 07:55 AM

Since a couple of days I'm trying to send data from a 18F442 to a 16F690. The result displayed on the 7_segment displays is always the double as it should be. If I send the number 5 I get number 10 and so on. I use source boost with Microchip IDE. Strange is, that the command: "#pragma CLOCK_FREQ 8000000" doesn't make any effect, even if I set "Configuration Bits set in code". I have to select the clock always with OSCCON (osccon = 01110111b;) to get the desired 8 Mhz clock.
If I send the data from the 18F442 to a Hyper Terminal the result is correct and if I use the program with a 16F877A the program works fine.
For help I would be very pleased.
Martin




#include <system.h>




#ifdef _PIC16F690
#pragma DATA 0x2007, _INTRC_OSC_CLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_ON & _CP_OFF
& _BOR_OFF & _IESO_OFF & _FCMEN_OFF
#endif

#pragma CLOCK_FREQ 8000000 //this sets the clock to 4mhz for the PIC16F690

volatile bit gie@INTCON.GIE; // receive buffer registor.
volatile bit peie@INTCON.PEIE; // receive buffer registor
volatile bit Go_Done@ADCON0.2; // Note the the 16fxx chip puts this in bit 1.
volatile bit trmt@TXSTA.TRMT; // transmit shift registor.
volatile bit rcif@PIR1.RCIF; // receive buffer registor.
volatile bit rcie@PIE1.RCIE; // receive buffer registor.
volatile unsigned char ad_h@ADRESH;
volatile unsigned char ad_l@ADRESL;




unsigned int result;
unsigned int TEST = 1;
unsigned int Ch_No;
unsigned int Ch_;
unsigned RX_capture;
char xmit = 0;

void init_TX_RX()
{
baudctl = 00000000b; // BRG16 = Lo (8-bit).
rcsta = 10000000b; // set SPEN bit,serial port enabled & CREN desactivated.
txsta = 00100100b; // TXEN disabled, BRGH = Lo-speed, TX9 8-bit, Async mode.
spbrg = 51; // 9600 baud.
}


unsigned long Bin_A_BCD(unsigned num_bin)
{
unsigned desp = 0;
unsigned long rest,bcd = 0;

while(num_bin != 0)
{
rest = num_bin % 10;
rest <<= desp;
desp += 4;
bcd |= rest;
num_bin /= 10;
}
return(bcd);
}

//

const unsigned T7SEG[10] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x98}; //
array of 10 numbers.

unsigned BCD_A_7Seg(unsigned num_BCD)
{
if (num_BCD <= 9)
return(T7SEG[num_BCD]);
else
return(0xFF);
}
//

void delay_1(void)
{
unsigned long rtdo = 0;

for (rtdo = 0;rtdo < 256; rtdo++);
}
/*
void interrupt (void)
{
rcsta.4 = 1;
if (rcif == 1)
xmit = rcreg; // read data from receive buffer.
}
*/

void main(void)
{
osccon = 01110111b; // set 8 MHz INTOSC.
trisa = 00001000b; // RA3 VPP rest output. RA0 Q2, RA1 Q3, RA2 Q4, RA4 Q1,
(RA4 is channel)
trisb = 00100000b; // RB5 RX_in, RB7 TX_out.
trisc = 00000000b; // 7_digit display bus..
ansel = 00000000b;
anselh = 00000000b;
wpub = 00000000b; // disable Pull-Up.
gie = 1;
rcie = 1;
peie = 1;

init_TX_RX();


unsigned long tp;
unsigned tp1;
unsigned i;



char data=0x64;





Ch_No = 0x04;
rcsta.4 = 0;

while (1)

{
rcsta.4 = 1;
while (rcif == 0) // PIR1
{
Ch_No = 0x00;
}
Ch_No = 0x02;


xmit = rcreg; // read data from receive buffer.
rcreg = 0;
//rcsta.4 = 0;

tp = Bin_A_BCD(xmit); // Convierte el número 128 decimal a BCD natural.
tp1 = tp & 0xF; // Se extrae el dígito BCD de menor peso (8) para
porta = 00000100b; // RA2, Q4 Hi.
portc = BCD_A_7Seg(tp1); // Se escribe el número 8 en el puerto B
delay_1(); // Retardo que permite la visualización

tp1 = tp >> 4; // Se desplaza el contenido de la variable cuatro
tp1 = tp1 & 0xF;
porta = 00000010b; // RA1, Q3 Hi.
portc = BCD_A_7Seg(tp1);
delay_1();

tp1 = tp >> 8; // Se desplaza el contenido de la variable cuatro
porta = 00000001b; // RA0, Q2 Hi.
portc = BCD_A_7Seg(tp1); // Se escribe el número 1 en el puerto B
delay_1();


Ch_ = Ch_No;

tp = Bin_A_BCD(Ch_); // Convierte el número 128 decimal a BCD natural.
tp1 = tp & 0xF; // Se extrae el dígito BCD de menor peso (8) para
porta = 00010000b; // RA4, Q4 Hi.
portc = BCD_A_7Seg(tp1); // Se escribe el número 8 en el puerto B
delay_1(); // Retardo que permite la visualización.


}
}



Reply to this topic



  


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users