HiTECH PICC
This program uses the MPLAB SIM to compile and debug
it also uses the USART IO located under Debugge
Settings
USART IO
Completed for the 44 pin demo board for the Micro course.
#define _LEGACY_HEADERS
#include
#include
#include
Configuration bits found in pic16f887.h file
CONFIG1H
#pragma config FOSC = INTIO67
Oscillator Selection bits (Internal oscillator block, port function on RA6 and RA7)
#pragma config WDTEN = OFF
Watchdog Timer Enable bit (WDT is always enabled. SWDTEN bit has no effect)
#pragma config LVP = OFF
Single-Supply ICSP Enable bit (Single-Supply ICSP enabled)
Function Prototypes
global declaration for the functions created
void setUSART();
program starts execution here
* This is a program that demonstrates the use of conditional statements,
Loops, operators and data types.Use the code provided along with code samples from previous
classes (i.e. loops.c, conditions.c, datatypes.c)to finish the LAB1 assign.c program.
*
int main(void)
{
setUSART();
Function call for seting up the UART IO Window capability - will be discussed later.
Define a variable of type char. Name it i
char cCount;
define a vartiable of type char, Name it count
define a vartiable of type char, Name it cHeight
define a vartiable of type char, Name it cLength
define a vartiable of type char, Name it cArea
Assign a value to cCount to determine the loop code to execute ***student to change value and run***
Assign a value of 10 the cLenght
Assign a value of 15 the cHeight
based on the values assigned to variable cCount select an if() condition
Add additional else if() statement code to call the other loop functions
if(cCount == 1)
{
Perfom a for loop count to 10
use printf("") to display the iteration number to the UARTIO window on each loop
}
add an else if() statement for the while loop code to execute
Perform a while loop count to 8 inside the else if() statement. Uses {} to su
ound the code
use printf("") to display the iteration number to the UARTIO window on each loop
Add an else condition to calculate the area of a rectangle. Remember to us {} to su
ound your code
add code to calculate the area of a rectangle
use printf("") to display the result to the UARTIO window
printf("Program complete! Press Pause to terminate.");
while (1);
infinite loop to pause the program
}
************************************************************************
Code used to set up the USART ouput for display on the UART output screen
Will be discussed in detail late
************************************************************************
void setUSART()
{
Next four register values are determined from the BAUD rate tables
BRGH = 1;
TXSTA Reg, Chooses high speed Baud rate of development
BRG16= 0;
BAUDCTL reg. 8-Bit Baud generator is used
SPBRG = 51;
Baud selection for 9600. Obtained from the BAUD Rates for asynchronous modes tables
SYNC = 0;
TXSTA, Set the TX Control to asynchronous mode.
TXEN = 1;
Set to 1 to enable the USART Transmitte
INTCON = 0x00;
INTCON, Enables inte
upts General an peripheral.
TXIF = 0;
RX9 =0;
RCSTA, Sets 8 BIT reception
SPEN =1;
RCSTA, Serial Port enabled
CREN = 0;
RCSTA, continuous receive disabled
}
HiTECH PICC
This program uses the MPLAB SIM to compile and debug
it also uses the USART IO located under Debugge
Settings
USART IO
Completed for the 44 pin demo board for the Micro course.
#define _LEGACY_HEADERS
#include
#include
#include
PIC18F45K20 Configuration Bit Settings
'C' source line config statements
#include
#pragma config statements should precede project file includes.
Use project enums instead of #define for ON and OFF.
CONFIG1H
#pragma config FOSC = INTIO67
Oscillator Selection bits (Internal oscillator block, port function on RA6 and RA7)
#pragma config WDTEN = ON
Watchdog Timer Enable bit (WDT is always enabled. SWDTEN bit has no effect)
#pragma config LVP = ON
Single-Supply ICSP Enable bit (Single-Supply ICSP enabled)
Function Prototypes
void setUSART();
program starts execution here
* This is a program that demonstrates the use of confditional statements
programmer for doing calculations and bit manipulations.*
int main(void)
{
Variable declarations
char forCount, whileCount, doWhileCount;
Declare 3 loop count variables
char j, k;
Declare 3 loop iterators as type cha
time_t t;
int randnum = 0;
int targnum = 40;
int i;
Variable assignments
forCount = 15;
Assign values to the loop terminators variables
whileCount = 8;
doWhileCount = 10;
i = j =0;
Initialize loop iterators to 0
k = 100;
setUSART();
Call su
outine to set up SIM Uart1 window
For Loop example
for(i = 0;i<=forCount;i++)
{
printf("The for loop value of i = %d\n", i);
}
While Loop example
while(j <= whileCount)
{
printf("While loop number %d\n",j);
j++;
}
do...while loop example
do
{
printf("Do while loop number = %d\n", k);
k++;
}while(k<=doWhileCount);
srand(NULL);
for(i=0;i<1000 && (randnum != targnum);i++)
{
XXXXXXXXXXrandnum = rand()%49;
}
if(i == 1000)
XXXXXXXXXXprintf("Unable to produce the number in 1000 attempts\n\n");
else
XXXXXXXXXXprintf("The number of loops required to produce %d = %d\n\n",targnum, i);
printf("\n****Finished!!!****\nPress PAUSE and RESET to terminate and reset the program.\n");
while(1);;
}
Code used to set up the USART ouput for display on the UART output screen - Will be discussed in detail late
void setUSART()
{
Next four register values are determined from the BAUD rate tables
BRGH = 1;
TXSTA Reg, Chooses high speed Baud rate of development
BRG16= 0;
BAUDCTL reg. 8-Bit Baud generator is used
SPBRG = 51;
Baud selection for 9600. Obtained from the BAUD Rates for asynchronous modes tables
SYNC = 0;
TXSTA, Set the TX Control to asynchronous mode.
TXEN = 1;
Set to 1 to enable the USART Transmitte
INTCON = 0x00;
INTCON, Enables inte
upts General an peripheral.
TXIF = 0;
RX9 =0;
RCSTA, Sets 8 BIT reception
SPEN =1;
RCSTA, Serial Port enabled
CREN = 0;
RCSTA, continuous receive disabled
}
Microcontroller Programming - Class Exercise 2B
Microcontroller Programming - Class Exercise 2B
Nova Scotia Community College
Waterfront Campus
Electronics Engineering Technician
_______________________________________________________________
EETD 4001 Microcontroller Programming I
Assignment # 2 – Loops and Conditional Statements
Date Assigned: ___________________________
Due Date: ___________________________
Note: Late assignments will be subject to a 20 percent per day penalty
___________________________________________________________________
Student Name _______________________________________
Student ID _______________________________________
Filename(s) _______________________________________
_________________________________________________
_________________________________________________
_________________________________________________
_________________________________________________
Date Submitted ________________________________________
Mark ________
The purpose of this programming lab is to have the student use the Datatypes, Operators, Conditions and Loops code given in previous lessons to write a program to perform the following operation:
1. As a starting point, use the sample piece of code provided in Brightspace located in the Introduction to ANSI C – Conditional Statements section, Assignment 2 – Loops/Conditions Source Code.
2. Use the handout that you were given ‘Creating a Project with UART capability’ to create a project using the assing2B.c code provided.
3. Once the new project has been created, compile the code to ensure the project created is a working project. Once it is working perform the following steps.
NOTE: Pay attention to the comments in the code. They will direct you as to what is required at certain steps
4. Define a variable of type char and name it cCount.
5. Define a loop iterator variable of type char and name it i.
6. Define a length variable of type char and name it cHeight.
7. Define a width variable of type char and name it cWidth.
8. Define an area variable of type char and name it cArea.
9. Assign the declared variables the following values
a. cCount = 1 (count will be updated for the different conditions)
. i = 0
c. cWidth = 15
d. cLength = 10
10. The cCount variable will be used as follows:
a. If cCount equals 1 then the first if() condition runs
. If cCount equals 2, the first else if() condition will run.
c. If cCount is equals 3, the second else_if() will run
d. If cCount is greater than three or less than one, the else condition will run
11. To do this, add a conditional if()/else if()/else statements to inte
ogate the value of cCount to determine what type of loop you will run.
a. If the first condition is satisfied run a for-loop of 10
. If the second condition is satisfied run a while-loop of 8
c. If the third condition is satisfied, run a do-while loop of 6
d. If none of the conditions are satisfied, calculate the area of a rectangle with a length equal to 10 and a width of 15 (see defined variable values).
(See example below).
if (count == 1)
{
for(iterator ;condition; increment)
run a for loop for a count of 10.
printf(“Value of the for Loop = %d\n”,iterator);
}
else if (condition)
{
}
And so on…
12. The value of the count and the type of loop it was counted in should be printed to the UART IO window as shown above. You can also use the printf () statements in the code you were given as examples
13. Evaluation of your program will be determined from your code and through demonstration of a working program.
a. Remember to format and comment your code as per the guidelines discussed in previous lectures
Submission:
· Combine a copy of your source (.c) code and UART1 Output window in an MSWord Document and submit to Brightspace.
EETD 4001 Page 2
#include
#include
#include "usart.h"
void
putch(unsigned char byte)
{
/* output one byte *
while(!TXIF) /* set when register is empty *
continue;
TXREG = byte;
}
unsigned char
getch() {
/* retrieve one byte *
while(!RCIF) /* set when register is not empty *
continue;
return RCREG;
}
unsigned cha
getche(void)
{
unsigned char c;
putch(c = getch());
return c;
}
#ifndef _SERIAL_H_
#define _SERIAL_H_
#define BAUD 9600
#define FOSC 4000000L
#define NINE 0 /* Use 9bit communication? FALSE=8bit *
#define DIVIDER ((int)(FOSC/(16UL * BAUD) -1))
#define HIGH_SPEED 1
#if NINE == 1
#define NINE_BITS 0x40
#else
#define NINE_BITS 0
#endif
#if HIGH_SPEED == 1
#define SPEED 0x4
#else
#define SPEED 0
#endif
#if defined(_16F87) || defined(_16F88)
#define RX_PIN TRISB2
#define TX_PIN TRISB5
#else
#define RX_PIN TRISC7
#define TX_PIN TRISC6
#endif
*