Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[MOVED] PIC16F628 | Hi-Tech | Interrupt based UART Problem

Status
Not open for further replies.

ediamondsrt

Junior Member level 3
Joined
May 3, 2007
Messages
31
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,461
[SOLVED] PIC16F628 | Hi-Tech | Interrupt based UART Problem

Hello,

I am Using PIC16F628A and Hi-Tech Compiler for MPLAB I want to write routine for UART in which i want Receiving interrupt based,
Here is Initialization i have done for UART

Code:
        TXSTA = 0x24;
	SYNC = 0;
	SPEN = 1;
	TXEN = 1;
	BRGH = 1;
	SPBRG = 129;

	SREN = 0;
	RCIE = 1;		// Interrupt Enable
	CREN = 1;
	GIE = 1;

Transmission works Perfectly and even polling based receiving works fine can anybody give me code for interrupt based routine please.
 
Last edited:

hello ediamondsrt,



Code:
TXSTA = 0x24;   -----> are you using synchronous mode ???
	SYNC = 0;
	SPEN = 1;
	TXEN = 1;
	BRGH = 1;
	SPBRG = 129;

***** you have to enable Peripheral Interrupt Enable bit****
Code:
SREN = 0;        
	RCIE = 1;		// Interrupt Enable
	CREN = 1;
	GIE = 1;
i had enclosed interrupt driven UART receiver code .... it may help you


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//_______________________________________________________________
//                  UART_RX_CODE_16f628a
//________________________________________________________________
 
#include <pic.h>
 
//___________________________________________________
//                  PORT INIT
//___________________________________________________
 
 
#define init_ports() PORTA=0x00,TRISA=0x00,\
                     PORTB=0x00,TRISB=0x02;
//___________________________________________________
//                  UART INIT
//___________________________________________________
 
#define init_uart() SPBRG=0x67,TXEN=0,TXSTA=0x04,\
                    RCSTA=0x90,INTCON=0x40,PIE1=0x20;
 
//BRGH = 1
//SPEN = 1,CREN = 1,
//RCIE = 1,PEIE = 1 -- Peripheral Interrupt Enable bit
//___________________________________________________
//                  GENERAL INIT
//___________________________________________________
 
 
 
bit  Data_Rce_Flag,Err_FERR_flag=0,Err_OERR_flag=0;
        
unsigned char Res_Buff[10],cnt,Err_Buff,Data_Cnt;
 
 
#define   PORTBIT(adr, bit)   ((unsigned)(&adr)*8+(bit))
static bit Ten_Data_Rec_Ind  @   PORTBIT(PORTB, 5);// Pin RB5 will indicate successful data reseve
 
 
//___________________________________________________
//                  Intrrupt Routine 
//___________________________________________________
 
static void interrupt isr(void)
{
    if(RCIF)
    {   
        
        RCIF=0;
        if(FERR)
        {
            Err_Buff=RCREG;
            Err_FERR_flag=1;
        }
        else if(OERR)
        {
            CREN=0;
            CREN=1;
            Err_OERR_flag=1;
        }
        if(!(Err_FERR_flag & Err_OERR_flag))
        {
        
        
                Res_Buff[Data_Cnt]=RCREG;
                Data_Cnt++;
            if(Data_Cnt > 0x0A)
            Data_Rce_Flag =  1;
        }
        Err_FERR_flag=0;
        Err_OERR_flag=0;
    }
}
 
 
 
///________________________________________________________  
//                        DELAY 
//_________________________________________________________
 
void delay(unsigned char cnt)
{
    unsigned long int count=cnt;
    count=count*2;
    while(count--);
}
//_________________________________________________________
//                           MAIN 
//_________________________________________________________
 
void main()
{
init_ports();
init_uart();
GIE=0;
delay(10);
GIE=1;
while(1)
{
    if(Data_Rce_Flag)
    {   
        Data_Rce_Flag=0;
        Ten_Data_Rec_Ind = 1;
        
    }
}   
}



regards
amar
 
Last edited by a moderator:
hello ediamondsrt,



Code:
TXSTA = 0x24;   -----> are you using synchronous mode ???
	SYNC = 0;
	SPEN = 1;
	TXEN = 1;
	BRGH = 1;
	SPBRG = 129;

***** you have to enable Peripheral Interrupt Enable bit****
Code:
SREN = 0;        
	RCIE = 1;		// Interrupt Enable
	CREN = 1;
	GIE = 1;
i had enclosed interrupt driven UART receiver code .... it may help you


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//_______________________________________________________________
//                  UART_RX_CODE_16f628a
//________________________________________________________________
 
#include <pic.h>
 
//___________________________________________________
//                  PORT INIT
//___________________________________________________
 
 
#define init_ports() PORTA=0x00,TRISA=0x00,\
                     PORTB=0x00,TRISB=0x02;
//___________________________________________________
//                  UART INIT
//___________________________________________________
 
#define init_uart() SPBRG=0x67,TXEN=0,TXSTA=0x04,\
                    RCSTA=0x90,INTCON=0x40,PIE1=0x20;
 
//BRGH = 1
//SPEN = 1,CREN = 1,
//RCIE = 1,PEIE = 1 -- Peripheral Interrupt Enable bit
//___________________________________________________
//                  GENERAL INIT
//___________________________________________________
 
 
 
bit  Data_Rce_Flag,Err_FERR_flag=0,Err_OERR_flag=0;
        
unsigned char Res_Buff[10],cnt,Err_Buff,Data_Cnt;
 
 
#define   PORTBIT(adr, bit)   ((unsigned)(&adr)*8+(bit))
static bit Ten_Data_Rec_Ind  @   PORTBIT(PORTB, 5);// Pin RB5 will indicate successful data reseve
 
 
//___________________________________________________
//                  Intrrupt Routine 
//___________________________________________________
 
static void interrupt isr(void)
{
    if(RCIF)
    {   
        
        RCIF=0;
        if(FERR)
        {
            Err_Buff=RCREG;
            Err_FERR_flag=1;
        }
        else if(OERR)
        {
            CREN=0;
            CREN=1;
            Err_OERR_flag=1;
        }
        if(!(Err_FERR_flag & Err_OERR_flag))
        {
        
        
                Res_Buff[Data_Cnt]=RCREG;
                Data_Cnt++;
            if(Data_Cnt > 0x0A)
            Data_Rce_Flag =  1;
        }
        Err_FERR_flag=0;
        Err_OERR_flag=0;
    }
}
 
 
 
///________________________________________________________  
//                        DELAY 
//_________________________________________________________
 
void delay(unsigned char cnt)
{
    unsigned long int count=cnt;
    count=count*2;
    while(count--);
}
//_________________________________________________________
//                           MAIN 
//_________________________________________________________
 
void main()
{
init_ports();
init_uart();
GIE=0;
delay(10);
GIE=1;
while(1)
{
    if(Data_Rce_Flag)
    {   
        Data_Rce_Flag=0;
        Ten_Data_Rec_Ind = 1;
        
    }
}   
}



regards
amar

Hey Amar,

Thanks a lot for the help. I too come to know about that PEIE bit later. Thanks a lot for help.

---------- Post added at 07:33 ---------- Previous post was at 07:24 ----------

My problem is solved Moderator can Change Prefix.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top