Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC16F UART aaaand MikroC

Status
Not open for further replies.

Vizier87

Active Member
Hi guys,

I know, I know, PIC16Fs are waaay outdated now but it's just a prototype and I'll be migrating to MSP430s soon so I just want to get some programming lessons here.

So... here's my UART program for a PIC1F877A:

C:
void main() {
 /////////////////////////
 rp1_bit=0;       ////////
 rp0_bit=1;       //BANK 1
 /////////////////////////
 
 adcon1=0b00000111;  //pcfg settings: all digital I/O
 cmcon= 0b00000111;  //comparators off
 cvrcon=0;           //voltage comparators off
 txsta=0b00000100;   //8-bit, Async mode, Hi-Speed, enable transmission
 intcon=0b11000000;  //enable all peripheral interrupts
 spbrg=64;           //19.231 kBps
 txie_bit=1;         //Tx int. flag enable

 trise=0x07;         //porte=input, pspmode_bit=0;
 trisa=0b100000;     //ra5=input
 trisd=0xff;         //rd4=input, rest output
 trisb=0;            //portb=output
 trisc=0b10111111;   //input for keypads and RX=input/TX=0utput

 
 /////////////////////////
 rp1_bit=0;       ////////
 rp0_bit=0;       //BANK 0
 /////////////////////////

 pir1=0b11000000;
 rcsta=0b10011100;  // enable Rx/Tx, 8-bit, continuous, 
                    //address detect, interrupt enable,
                    //framing error, no overrun

 portb=0;
 porta=0;

   while (1){
   
     /////////////////////////
     rp1_bit=0;       ////////
     rp0_bit=0;       //BANK 0
     /////////////////////////
     
     
     if (ra5_bit==1){
        rb7_bit=1;
        
         /////////////////////////
         rp1_bit=0;       ////////
         rp0_bit=1;       //BANK 1
         /////////////////////////
        
          if (txif_bit==1){

            txen_bit=1;
            txreg=0xff;

            }
        }

     else {
          
          /////////////////////////
          rp1_bit=0;       ////////
          rp0_bit=1;       //BANK 1
          /////////////////////////

          txen_bit=0;

          /////////////////////////
          rp1_bit=0;       ////////
          rp0_bit=0;       //BANK 0
          /////////////////////////
     
          rb7_bit=0;
          

     }
     
   }
   
}

And here's the memory org:

**broken link removed**

Basically I'm only trying to make it like this:
I press on my keypad (RA5 in this case) and an LED turns on, and at the same time transmits 0xFF.
When the keypad is not pressed, it's idle. Simple.

Here's two questions:

1. I managed to get the Tx output nicely using a Logic Analyzer, BUT the output comes out without me pressing the button!

2. I am a bit unsure about whether we need to switch banks when polling a bit from another bank when I'm in a different bank. I know if we need to SET or CLEAR the bit, the bankswitch is needed, but in this case I need some clarification.

So guys, dig in and thanks in advance.
Appreciate the time spent on this. :)

Vizier87
 
Have a read through the MikroC help file. It has inbuilt functions for reading a key (that include debounce) and inbuilt functions to let you very easily setup the USART and also to send bytes through the USART. The help file contains code examples.
 
Thanks for the input guys.
Ian: Strangely, even if I didn't put in any value to the TXREG, a steady square signal is produced in TX.. Mystery. I thought data will only be transferred only if register is loaded?
 
okay, it worked good this time. I turned on TXIE, my bad.
Does it affect the signal quality by adding an LED indicator as well as turning the transmission on when I press the button?
 
Pommie (and myself) have said you don't need to mess with bank switching, of the TXREG register or any manual USART stuff as the compiler does all that for you. You seem to have ignored my post?

Please read the MikroC help file. Under; Contents->MikroC Libraries->USART;
USART Library
USART hardware module is available with a number of PICmicros. mikroC USART Library provides comfortable work with the Asynchronous (full duplex) mode.

You can easily communicate with other devices via RS232 protocol (for example with PC, see the figure at the end of the topic – RS232 HW connection). You need a PIC MCU with hardware integrated USART, for example PIC16F877. Then, simply use the functions listed below.

Library Routines;
Usart_Init
Usart_Data_Ready
Usart_Read
Usart_Write


Code:
  // Example This will initialize hardware USART and establish the communication at 2400 bps:
  Usart_Init(2400);

It really is that easy!

And to send a byte;
Code:
Usart_Write(0xAA);    /* send the byte 0xAA via USART */

The MikroC compiler has been designed to let a beginner work REALLY easily with all the PIC peripheral hardware modules. It's really worthwhile reading the MikroC compiler help file! There is probably a built in library routine for just about everything you would want to do. :)
 
Last edited:
Pommie (and myself) have said you don't need to mess with bank switching, of the TXREG register or any manual USART stuff as the compiler does all that for you. You seem to have ignored my post?

Please read the MikroC help file. Under; Contents->MikroC Libraries->USART;
USART Library
USART hardware module is available with a number of PICmicros. mikroC USART Library provides comfortable work with the Asynchronous (full duplex) mode.

You can easily communicate with other devices via RS232 protocol (for example with PC, see the figure at the end of the topic – RS232 HW connection). You need a PIC MCU with hardware integrated USART, for example PIC16F877. Then, simply use the functions listed below.

Library Routines;
Usart_Init
Usart_Data_Ready
Usart_Read
Usart_Write


Code:
  // Example This will initialize hardware USART and establish the communication at 2400 bps:
  Usart_Init(2400);

It really is that easy!

And to send a byte;
Code:
Usart_Write(0xAA);    /* send the byte 0xAA via USART */

The MikroC compiler has been designed to let a beginner work REALLY easily with all the PIC peripheral hardware modules. It's really worthwhile reading the MikroC compiler help file! There is probably a built in library routine for just about everything you would want to do. :)

Hiya MrRb,
You did not mention anything the necessity about bank switching did you? Pommie cleared that, (thanks Pommie, I just forgot to quote his post before) but if you did then I failed to extricate the statement in your reply.

Sorry for giving you the wrong impression there, I did open the help file but I just find myself difficult to adopt this whole library thing since I learnt to access registers directly in all my previous programs since I started with MikroC (that was 3 months ago :)). I'll take a look into it soon, God willing.

@Ian: Errr.... It is on a different pin actually. :( Well it helped a lot to see the LED light up when I push the button.

Well guys thanks for the time spent on this. I'll come back later, (hopefully with more knowledge about this library whatsits.) :D

Vizier87
 
I should have been more clear, my point was that Pommie and myself had made comments about leaving the fussy register stuff behind and letting the compiler handle stuff for you.

I did not mean to sound condescending or critical, please accept my apology for any misunderstanding. Sometimes things typed in a bit of a hurry (like a quick suggestion to help someone) can come across the wrong way! Good luck with the project. :)
 
I should have been more clear, my point was that Pommie and myself had made comments about leaving the fussy register stuff behind and letting the compiler handle stuff for you.

I did not mean to sound condescending or critical, please accept my apology for any misunderstanding. Sometimes things typed in a bit of a hurry (like a quick suggestion to help someone) can come across the wrong way! Good luck with the project. :)

No offense taken there MrRB. Thanks for the feedback.
All's good for now. I'm experimenting with Xbees for now, I'll get back to this thread soon.

Thanks guys.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top