Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
trying to get this working on non-uart pin(RC1) for LCD NHD-0216K3Z-NSW-BBW-V3
with RS-232 (TTL) protocol, 8-bit data, 1 Stop bit, no parity, no hand-shaking. BAUD rate is 9600
I am getting results but they are a "bit" or 2 off, I think my problem is with delay time, or maybe inversion issue.
Maybe i need to tweak my clock? or set clock up wrong? Maybe im not factoring in properly the time it takes for all the if checking?
tried lots of small adjustments but just not getting it..
please advise if im being over or under sensitive about these things!
thanks!
bad function:
Code:void sendUart(unsigned char datdat) { PORTC |= 0b00000010; // start bit __delay_us(99); nextstep: if (datdat&1){PORTC |= 0b00000010; }else{PORTC &= 0b11111101;} set output __delay_us(99); datdat = datdat>>1; if (cnt<8){cnt++;goto nextstep;} // do 8 times PORTC &= 0b11111101; // stop bit __delay_us(99); }
setup code:
Code:#include <xc.h> #define _XTAL_FREQ 8000000 main(){ OSCCONbits.SCS = 0; // Clock source defined by FOSC<2:0> of the Configuration Word OSCCONbits.IRCF0 = 1; // 8 Mhz pls OSCCONbits.IRCF1 = 1; OSCCONbits.IRCF2 = 1; OSCTUNE= 0b00000000; // no drift INTE = 1; // interrupt used while(1){} } void interrupt INTERRUPT() { if(INTF == 1 & INTE == 1) // on interrupt { unsigned char dat = 0xAA; // test byte to send to LCD unsigned char cnt=0; sendUart(dat); delayMS(1000); // long pause INTF = 0; } }
C, asm, pascal, basic... It will work..... However!! If there is an OSCTUNE register without calculating an offset, there is little chance of getting a specific speed... There are little programs that will generate the offset required to get the crystal accurate..Try doing this in asm instead of a higher level language. It's almost certain it will work if you calculate your delays right and check any frequency calibration byte needed.
C, asm, pascal, basic... It will work..... However!! If there is an OSCTUNE register without calculating an offset, there is little chance of getting a specific speed... There are little programs that will generate the offset required to get the crystal accurate..
Here is an example... I have just completed a wireless radio unit 9600 baud... I used the internal OSC of a pic18f4520... Nope!!! Put a 4Mhz crystal on..... Works like a charm..
Just to throw in another spanner... If the timing is a little out AND there is another little bug!! You'll be there forever... As there is nothing worse than two unknown issues.... Get the timing right and then you'll see if something else is screwing up the works..
Here is an example... I have just completed a wireless radio unit 9600 baud... I used the internal OSC of a pic18f4520... Nope!!! Put a 4Mhz crystal on..... Works like a charm..
No... It was set to zero.. I didn't check whether it was stored elsewhere... I have checked four or five of them... I couldn't be bothered messing so I just put a crystal on...Doesn't the 18F4520 come with the correct osctune value already loaded?,
datasheet said:2.6.5.1
Compensating with the USART
An adjustment may be required when the USART
begins to generate framing errors or receives data with
errors while in Asynchronous mode. Framing errors
indicate that the device clock frequency is too high; to
adjust for this, decrement the value in OSCTUNE to
reduce the clock frequency. On the other hand, errors
in data may suggest that the clock speed is too low; to
compensate, increment OSCTUNE to increase the
clock frequency.
I think what i was referring to was actually the OSCAL value? It's been a while since i had to do this so i forgot exactly which variable it was, but one example chip was the 12F675 and that had the value i was talking about.
void delayUS(unsigned int t)
{
while (t>999){__delay_us(10000);t-=1000;}
while (t>99){__delay_us(1000);t-=100;}
while (t>9){__delay_us(100);t-=10;}
while (t>0){__delay_us(10);t-=1;}
}
void LCDascii(unsigned char dat)
{
unsigned char cnt;
unsigned char datdat;
unsigned int baud=0x133;
// for (baud=0x1;baud<2000;baud++){
// dat = 0xAA;
// unsigned char datdat;
datdat = dat;
cnt=0;
PORTC &= 0b11111101;
delayUS(baud);
nextstep:
if (datdat&1){PORTC |= 0b00000010; }else{PORTC &= 0b11111101;}
delayUS(baud);
datdat = datdat>>1;
if (cnt<8){cnt++;goto nextstep;}
dummy |= 0b00000010;
PORTC = dummy;
delayUS(baud); delayUS(baud); delayUS(baud); delayUS(baud); delayUS(baud);
delayUS(baud); delayUS(baud); delayUS(baud); delayUS(baud); delayUS(baud);
// eeprom_write(1, ((baud>>8)));
// eeprom_write(2, (baud)&0xFF);
delayMS(100);
// }
}
Oops.. up till my attempts today , i have been cranking the clock to max, then osctune to max, faster=better right?
Certainly those come pre-aligned, and you need to save and restore it's value when you program the chip - 'hopefully' MPLAB does that automatically?.
And I also wonder , how different are the architectures , if my PIC16F688 (no LATS) has 35 instructions, does it change with pic 18? ... and what does that mean about x86? ... and Arduino? different instructions/hex mapping .. hex code changes between devices right?
Welcome to our world... Hitachi H8/500 16 bit.... The developer couldn't understand S records so thats how he did it!!! Nightmare!!!manually entering the hex bytes one by one
Welcome to our world...