Page 1 of 2

USART without "USART.h"

PostPosted: Sun Aug 03, 2014 11:38 am
by Dimebag
I have been reading the datasheet for 18F452. I am just trying to make a start on using the datasheets to program in C without using the (read.c) and (write.c).
Code: Select all
#include <xc.h>

//PIC18F452
//Configurations and Fuses
#pragma config  OSC = HS, OSCS = OFF,PWRT = ON,BOR = OFF
#pragma config  BORV = 45,WDT = OFF,WDTPS = 1,CCP2MUX = OFF
#pragma config  STVR = OFF,LVP = OFF,DEBUG = OFF
#pragma config  CP0 = OFF,CP1 = OFF,CP2 = OFF,CP3 = OFF,CPB = OFF,CPD = OFF
#pragma config WRT0 = OFF,WRT1 = OFF,WRT2 = OFF,WRT3 = OFF,WRTB = OFF,WRTC = OFF,WRTD = OFF
#pragma config EBTR0 = OFF,EBTR1 = OFF,EBTR2 = OFF,EBTR3 = OFF,EBTRB = OFF

//PORTB pins.
int delayselect[9] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
//Timer values.
char offset[20] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
    0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};

int temp = 0;
int inside = 0;
int outside = 0;
int scanPin = 600;
int DelayA = 580;
int DelayB = 32;

void Initialization(void) {
    LATA = 0x00;
    LATB = 0X00;
    LATC = 0X00;
    LATD = 0X00;
    LATE = 0X00;

    TRISA = 0x00;
    TRISB = 0x00;
    TRISC = 0x80;
    TRISD = 0x00;
    TRISE = 0x00;

    PORTA = 0x00;
    PORTB = 0x00;
    PORTC = 0x00;
    PORTD = 0x00;
    PORTE = 0x00;
}

void usart_setup(void) {
    //TRANSMIT STATUS AND CONTROL REGISTER
    TXSTAbits.TXEN = 1;
    TXSTAbits.BRGH = 1;
    TXSTAbits.TX9 = 0;
    //RECEIVE STATUS AND CONTROL REGISTER
    RCSTAbits.SPEN = 1; //Set
    RCSTAbits.CREN = 1; //Set
    //Peripheral Interrupt Enable bits
    INTCONbits.PEIE = 0; //Clear
    INTCONbits.GIE = 0; //Clear

    PIR1bits.RCIF = 0; //Clear
    PIE1bits.RCIE = 0; //Clear
    SPBRG = 129;

}

void testup(void) {
    PORTB = 0x01;
}
void main(void) {

    Initialization(); //Set all device pins.
    usart_setup();
    TXREG = offset[2];
    while (1) {

        INTCONbits.GIE = 0;
        if (PIR1bits.RCIF == 0)

        Initialization();
        testup();

    }

}

Re: USART without "USART.h"

PostPosted: Sun Aug 03, 2014 4:25 pm
by Olin Lathrop
Dimebag wrote:I have been reading the datasheet for 18F452. I am just trying to make a start on using the datasheets to program in C without using the (read.c) and (write.c).

Um, OK, why are you telling us this?

Re: USART without "USART.h"

PostPosted: Sun Aug 03, 2014 5:13 pm
by Tom Maier
He's telling us this because it is what people have told him not to do. :lol:

Dimebag is a troubled teenager who is leaving the protective environment of highschool and is planning to go to college. But, he has learning issues caused by his teenage rebellion. My guess is that either his parents or his highschool teachers were pretty rough on him and he has mentally retreated into this offensive game of "Schlemiel" as a coping mechanism. Now he is stuck in it and can't find his way out.

Try to help dimebag at your own peril... he will get you back for your attempts.

My son just finished his first year of college and was able to shake this off. I was pretty worried about him and had a series of talks with him about it. He was doing it because his mother was verbally pounding on him for years. He was schlemieling every adult he could find. Now he understands what he was doing and why he was doing it, so he only uses it under the right circumstances directed towards the right people.

Dimebag hasn't reached that stage of enlightenment yet. He reacts in a negative way to all "helpful" adults in the same way.

Usually this game is started by a domineering parent trying to "help" a rebellious teenager. If it is only one teacher that causes it, then the game peters out a couple months after the class ends. If it is a parent, the game continues for a few years after the child leaves home. It is a common cause of students flunking college.

Re: USART without "USART.h"

PostPosted: Sun Aug 03, 2014 9:23 pm
by Olin Lathrop
Tom Maier wrote:He's telling us this because it is what people have told him not to do.

My point was more that he neglected to ask a question. I assumed this to be a oversight.

People need to learn to ask what they want to know, not employ silly word games. For example, stupidly asked questions like "Does anyone know ..." deserve short answers like "Yes, surely someone does". Unfortunately on busy forums there is always some wannabe that can't resist the temptation to look smart and blurts out the answer anyway.

In this case, I just wanted to waste his time and delay gratification until he asked the question properly. However, I'm not likely to wade thru many lines of code, but that's the next issue. I was going to wait to mention that until after the question is a actual question. I don't really give a crap if anyone's sily-ass problem here is actually solved, but I do care about the quality of questions and answers here, so this method works just fine for me. Perhaps if it eventually becomes a comprehensible question that is easily understood, doesn't disrespect those he is seeking free help from, and isn't too much trouble to dig into, I might even answer it. Until then, we need to make this a frustrating experience by withholding any real help. If his game is "poor little me, oops, I didn't know I wasn't supposed to do that", he's never going to get any help, at least not from me.

Re: USART without "USART.h"

PostPosted: Sun Aug 03, 2014 9:50 pm
by Tom Maier
If his game is "poor little me, oops, I didn't know I wasn't supposed to do that", he's never going to get any help, at least not from me.


Yes, that's his game.

He asks for help, then does the opposite of what you told him.
Then he apologies.
He makes illogical statements.
Then he apologies.
Then he asks for more help.
Then he pretends he can't hear you.
Then he apologies.

The "dimebag" moniker is to show that he is on dope, and that is part of the game, too. You are supposed to pity him and forgive him because his is all doped out.

It's all about pissing people off and then seeking forgiveness, in an endless loop.

He isn't able to read these lines. It's part of the game.

Re: USART without "USART.h"

PostPosted: Sun Aug 03, 2014 9:55 pm
by ric
Olin, your board email address is bouncing forum notification emails while you're on "holiday".... :)

Re: USART without "USART.h"

PostPosted: Sun Aug 03, 2014 11:35 pm
by Olin Lathrop
ric wrote:Olin, your board email address is bouncing forum notification emails while you're on "holiday".... :)

I looked earlier, and couldn't find a way to turn off all subscriptions or automatic subscriptions. However, it also appeared there weren't any, so I didn't think the forum software would be trying to send me email. It hasn't in the last few weeks that I recall.

In any case, I changed my forum email address to point to the special temporary address that will be active while I'm away. Now I have to remember to undo that when I get back.

Re: USART without "USART.h"

PostPosted: Sun Aug 03, 2014 11:38 pm
by Olin Lathrop
I should have mentioned that part of this trip includes the Microchip Masters conference in Phoenix. I'm going to see a customer in Colorado Springs this week, then I have 1 1/2 weeks to bump around Colorado, New Mexico, Utah, and Arizona, until I have to show up at Masters on the morning of Wednesday 20 Aug.

Anyone else going to Masters?

Re: USART without "USART.h"

PostPosted: Mon Aug 04, 2014 8:49 am
by Dimebag
I posted the code after reading through the 18F452 datasheet section on USART. I wanted to see if I could code the registers correctly without using “usart.h”. I figured it would be easy to try this on a chip that I have already. I setup a breadboard with the computers hyper terminal to see if the transmit would work. I had some problems with the baud rate settings, so I checked the datasheet again and found I had put the wrong decimal value in SPBRG.

Re: USART without "USART.h"

PostPosted: Mon Aug 04, 2014 1:33 pm
by Olin Lathrop
Dimebag wrote:I posted the code ...

In other words: "Looky me world! I done progammed me a PIC!"

This is the 16 bit core forum. For show and tell, go back to 5th grade.