CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

18f46k22 interrupt no work
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
daniel82



Joined: 05 Jan 2011
Posts: 11

View user's profile Send private message

PostPosted: Tue Jan 11, 2011 8:25 pm     Reply with quote

My PC is locked by IT dept, I will upgrade it but really take time to get all the internal approval, the project really urgent, can you help me to look into first? Thank you very much!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jan 12, 2011 4:56 pm     Reply with quote

Here is work-around code for UART2 in the 18F46K22, for vs. 4.109.
I only have a 18F45K22, so I used that for testing. It works.

The CCS #use rs232() statement doesn't work for UART2 for vs. 4.109.
So, don't use it. Don't even put it into your program. (It's OK for UART1).
Set the baud rate for UART2 with a #define statement.

For UART2, use the putc2(), getc2(), and kbhit2() functions shown below.
Also, you must call the init_uart2() function one time at start of your
program. You can't use printf with "streams" for UART2. You can
use printf, but you must use the re-direction feature of CCS to send the
output to the putc2() function. All of this is demonstrated below.

The program below displays the following output in the terminal window.
I pressed "asdf" a few times on my keyboard to get the 2nd line.
Quote:

Hello World
asdfasdfasdfasdf


Code:

#include <18F45K22.h>
#fuses INTRC_IO,NOWDT,PUT,NOBROWNOUT,NOLVP
#use delay(clock=4000000)

#define BAUDRATE2 9600

#byte BAUDCON2 = 0xF70
#byte RCSTA2 = 0xF71
#byte TXSTA2 = 0xF72
#byte TXREG2 = 0xF73
#byte RCREG2 = 0xF74
#byte SPBRG2 = 0xF75
#byte SPBRGH2 = 0xF76
#byte PIR3   = 0xFA4

#bit TX2IF = PIR3.4
#bit RC2IF = PIR3.5
#bit CREN2 = RCSTA2.4

#byte ANSELD = 0xF3B
#bit  ANSD6 = ANSELD.6
#bit  ANSD7 = ANSELD.7

#byte TRISD = 0xF95
#bit TRISD6 = TRISD.6
#bit TRISD7 = TRISD.7

//---------------------------------
// The putc2() must be declared "inline" to prevent
// vs. 4.109 from inserting garbage into the code.
#inline
void putc2(int8 c)
{
while(!TX2IF);

TXREG2 = c;
}

//---------------------------------
// The getc2() must be declared "inline" to prevent
// vs. 4.109 from inserting garbage into the code.
#inline
int8 getc2(void)
{
int8 temp;
int8 retval;
   
while(!RC2IF);

temp = RCSTA2;
retval = RCREG2;

if(bit_test(temp, 1))
  {
   CREN2 = 0;
   CREN2 = 1;
  }
 
return(retval);
}

//---------------------------------

#define kbhit2() (RC2IF)

//---------------------------------
#int_rda2
void rda2_isr(void)
{
char c;

c = getc2();

putc2(c);
}

//-----------------------------------
void init_uart2(void)
{
// Set the hardware UART2 pins to be digital i/o pins.
ANSD6 = 0;
ANSD7 = 0;

// Set the hardware UART2 pins to be inputs, per the PIC
// data sheet.
TRISD6 = 1;
TRISD7 = 1;

// Setup the UART2 baud rate generator and the other
// control registers.
SPBRG2 = (getenv("CLOCK") / (BAUDRATE2 * 16)) -1;
TXSTA2 = 0x26;
RCSTA2 = 0x90;
}

//======================================
void main(void)
{
init_uart2();  // Call this to setup UART2

// Printf statements for the 2nd UART must be done
// in the following way, with re-direction to putc2.
printf(putc2, "Hello World\n\r");

enable_interrupts(INT_RDA2);
enable_interrupts(GLOBAL);

while(1);
}


If you have any more bugs, I think you need to upgrade the compiler.
I don't want to do any more extensive work-arounds like this.
(I don't work for CCS).
daniel82



Joined: 05 Jan 2011
Posts: 11

View user's profile Send private message

PostPosted: Thu Jan 13, 2011 1:33 am     Reply with quote

Thank you very very much Exclamation
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group