100% found this document useful (2 votes)
233 views13 pages

XC8 Tutorial Ee3954 Fall12 13 C Part2

EmbeddedC. Microprocessors and microcontrollers - Part 2 EE3954 by Maarten Uijt de haag, tim bambeck References MPLAB(r) xc8 c compiler user's guide. Some functions are defined in the library, but are user-definable.

Uploaded by

varuntejavarma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
100% found this document useful (2 votes)
233 views13 pages

XC8 Tutorial Ee3954 Fall12 13 C Part2

EmbeddedC. Microprocessors and microcontrollers - Part 2 EE3954 by Maarten Uijt de haag, tim bambeck References MPLAB(r) xc8 c compiler user's guide. Some functions are defined in the library, but are user-definable.

Uploaded by

varuntejavarma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 13

EmbeddedC.

1
Microprocessors and Microcontrollers

Embedded C Programming Part 2

EE3954
by
Maarten Uijt de Haag, Tim Bambeck
References
MPLAB XC8 C Compiler Users Guide
EmbeddedC.2
Loops
EmbeddedC.3
int ii;
char array[10];

for(ii=0;ii<10;ii++)
{
array[ii] = ii;


}
Check MPLAB X IDE -> Window -> Output->
Disassembly Listing File
10-byte character array
When defining an array of characters, integers, etc., remember the size
limitations of the data memory.

For example, int array[100] represents 200 bytes, and will be filing up a
large portion of your data memory; actually, the compiler will probably
not let you define an array that size.
Loops
EmbeddedC.4
bit flag;



flag = determineFlag()

while(flag)
{



flag = determineFlag();

}

bit flag;





do
{



flag = determineFlag();

} while(flag);

Infinite Loop
EmbeddedC.5

while(1)
{





}

Condition is always true, program
within loops keeps getting executed.
User-definable Functions
Some functions are defined in the library, but are
user-definable.
Example:
EmbeddedC.6
printf("\r\n\r\n\tM\tMain Menu\r\n\r\n");
Would print out the first line of the Main menu for lab #5 on a usual program
\r : carriage return (ASCII code 0x0d)
\n : line feed (ASCII code 0x0a)
\t : tab (ASCII code 0x09)
User-definable Functions
EmbeddedC.7
printf
Relies on the users definition of function putch
In other words, for each character in the string
which is in the argument of the printf, the function
putch is called.
For example, when using UART serial communication, putch could be:
void putch(char byte)
{
while(!TXIF) continue;
TXREG = byte;
}
Wait until the TXIF flag
is set (= 1)
User-definable Functions
EmbeddedC.8
putch(0x0c);
Can use it by itself now as well
void putch(char byte)
{
while(!TXIF) continue;
TXREG = byte;
}
To send a clear screen ASCII character
User-definable Functions
EmbeddedC.9
getch*
For getting characters from an I/O device
Empty stub
char getch()
{
while(!RCIF) continue;

return RCREG;
}
Wait until the RCIF flag
is set (= 1)
For example, when using UART serial communication, getch could be:
*could be used with C scanf function.
User-definable Functions
EmbeddedC.10

char rxbyte;

void interrupt my_isr(void)
{
if(RCIE && RCIF)
{
rxbyte = getch();
}
}
Example:
Only for UART receive interrupts
Reading clears the flag
IMPORTANT: ONE SHOULD PERFORM OVERRRUN AND FRAME
ERROR CHECK AS WELL FOR ROBUSTNESS
FYI
Serial Communication Initialization:
EmbeddedC.11
// Setup the USART transmitter/receiver
CSRC = 0;
TX9 = 0;
TXEN = 1;
SYNC = 0;
BRGH = 1;
TRMT = 1;
TX9D = 0;

SPBRG = 12;

SPEN = 1;
RX9 = 0;
SREN = 0;
CREN = 1;
ADDEN = 0;
FERR = 0;
OERR = 0;
RX9D = 0;
Address all TXSTA and
RCSTA bits individually
Alternative: initialize the whole
bytes:

TXSTA = 0b00100110;
SPBRG = 12;
RCSTA = 0b10010000;
Pointers
EmbeddedC.12
printf(Hello PIC programmers");
String of characters
Can be defined as well by the following declaration:
const char *str = Hello PIC programmers;
So, string is stored in
program memory
So now printf(str);
pointer to a constant character; str is the
program memory address of this string.
Pointers
EmbeddedC.13
unsigned char a;
unsigned char b;

void func(char *num1, char *num2)
{
*num1 = *num1+6;
*num2 = *num2+10;
}

void main(void)
{
a = 1;
b = 2;
func(&a, &b);
}
Address of a Address of b
void func(char *num1, char *num2)
{
*num1 = *num1+6;
*num2 = *num2+10;
}
Pointer to address of a Pointer to address of b
a= a+6;
b= b+10;

You might also like