0% found this document useful (0 votes)
7 views18 pages

Micro C Embedede

The document is a students' practice manual for Embedded Control Systems, detailing various program codes for controlling LEDs, timers, and ADCs using PIC microcontrollers. It includes examples of interrupt handling, PWM control, and displaying ADC results on LEDs. The code snippets are designed for educational purposes within the Electro Mechanical Engineering Department at HU, showcasing practical applications of embedded systems programming.

Uploaded by

me777414
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views18 pages

Micro C Embedede

The document is a students' practice manual for Embedded Control Systems, detailing various program codes for controlling LEDs, timers, and ADCs using PIC microcontrollers. It includes examples of interrupt handling, PWM control, and displaying ADC results on LEDs. The code snippets are designed for educational purposes within the Electro Mechanical Engineering Department at HU, showcasing practical applications of embedded systems programming.

Uploaded by

me777414
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 18

Students Practice Manual: Embedded Control System for EME Students

Programme code for on/off leds


unsigned int k;
void main()
{
ADCON1=0X07;
ADCON0=0X00;
TRISB=0X00;
PORTB=0XFF;
Delay_ms(1000);
for(k=0;k<=10;k++)
{
switch (PORTB)
{
case 0x00:PORTB=0XFF;
Delay_ms(100);
break;
case 0xFF:PORTB=0X00;
Delay_ms(500);
break;
}
}

/* this programme code is used for timer0 as INTURPT */


unsigned int cnt;
void interrupt()
{
cnt++;
INTCON=0X20;
TMR0=96;
}
void main()
{
OPTION_REG=0X84;
ADCON1=0X07;
ADCON0=0X00;
PORTB=0X00;
TRISB=0X00;
TMR0=96;
INTCON=0XA0;
cnt=0;
while (1)
{
if(cnt==400)
{
PORTB=PORTB++;

BY: Yeshaw A. Electro Mechanical Engineering Dpt. HU 2016 1


Students Practice Manual: Embedded Control System for EME Students
cnt=0;
}

}
}

/* this programme code is used for timer1 for intrupt*/


unsigned int cnt;
void interrupt()
{
cnt++;
PIR1.TMR1IF = 0;
TMR1H = 0x80;
TMR1L=0x00;

}
void main()
{
ADCON1=0X07;
ADCON0=0X00;
PORTB=0X00;
TRISB=0X00;
T1CON=0X01;
TMR1H = 0x80;
TMR1L=0x00;
INTCON=0XC0;
PIE1.TMR1IE = 1;
PIR1.TMR1IF = 0;
cnt=0;
while (1)
{
if(cnt==76)
{
PORTB=PORTB++;
cnt=0;
}

}
}

BY: Yeshaw A. Electro Mechanical Engineering Dpt. HU 2016 2


Students Practice Manual: Embedded Control System for EME Students

/* this programme code is used for timer2 for intrupt*/


unsigned int cnt;
/*void interrupt()
{
cnt++;
PIR1.TMR2IF = 0;
TMR2=0;

} */
void main()
{
ADCON1=0X07;
ADCON0=0X00;
PORTD=0X00;
TRISD=0X00;
PORTC=0X00;
TRISC=0xFF;
T1CON=0X0B;
TMR1H = 0x80;
TMR1L=0x00;
//INTCON=0XC0;
// PIE1.TMR1IE = 1;
//PIR1.TMR1IF = 0;
cnt=3;
while (1)
{
if(TMR1L==cnt)
{
PORTD.F1=1;
}

}
}

/* this programme code is used for timer2 for intrupt*/


unsigned int cnt;
void interrupt()
{
cnt++;

BY: Yeshaw A. Electro Mechanical Engineering Dpt. HU 2016 3


Students Practice Manual: Embedded Control System for EME Students
PIR1.TMR2IF = 0;
TMR2=0;

}
void main()
{
ADCON1=0X07;
ADCON0=0X00;
PORTB=0X00;
TRISB=0X00;
T2CON=0XFF;
TMR2=0;
INTCON=0XC0;
PIE1.TMR2IE = 1;
PIR1.TMR2IF = 0;
cnt=0;
while (1)
{
if(cnt>=30)
{
PORTB=PORTB++;
cnt=0;
}

}
}

/* this programme code is used for RBO /EXTERNAL intrupt*/


unsigned int cnt;
void interrupt()
{
cnt++;
INTCON.F1=0;

}
void main()
{
OPTION_REG.F6=1;
ADCON1=0X07;
ADCON0=0X00;
PORTD=0X00;
TRISD=0X00;
PORTC=0X00;
TRISC=0xFF;
INTCON=0X90;
cnt=0;
while (1)
{

BY: Yeshaw A. Electro Mechanical Engineering Dpt. HU 2016 4


Students Practice Manual: Embedded Control System for EME Students
if(cnt==3)
{
PORTD.F1=1;
}

}
}

/* this programme code is used for RB4/7 /EXTERNAL intrupt*/


unsigned int cnt;
void InteRupt()
{
cnt++;
INTCON.RBIF=0;

}
void main()
{
ADCON1=0X07;
ADCON0=0X00;
PORTD=0X00;
TRISD=0X00 ;
PORTB=0X00;
TRISB=0x80;

INTCON.GIE=1;
INTCON.RBIE=1;
INTCON.RBIF=0;

cnt=0;
while (1)
{
if (Button(&PORTB,7,1,1))
{
InteRupt();
}
while (PORTB & 0x80) ;
if(cnt==3)
{
PORTD.F1=1;
}

}
}

BY: Yeshaw A. Electro Mechanical Engineering Dpt. HU 2016 5


Students Practice Manual: Embedded Control System for EME Students
/*
* Project name:
PWM1_Test_01 (PWM1 library Demonstration)
* Copyright:
(c) MikroElektronika, 2005-2008.
* Description:
This is a simple demonstration of PWM1 library, which is being used for
control of the PIC's CCP module. The module is initialized and started,
after which the PWM1 Duty Ratio can be adjusted by means of two buttons
connected to pins RA0 and RA1. The changes can be monitored on the CCP
output pin (RC2).
* Test configuration:
MCU: PIC16F877A
Dev.Board: EasyPIC4
Oscillator: HS, 08.0000 MHz
Ext. Modules: -
SW: mikroC v8.0.0.0
* NOTES:
- Pull-down PORTA and connect button jumper (jumper17) to Vcc.
- To verify hold down RA0 or RA1 and watch the LEDs.
*/

unsigned short current_duty, old_duty;

void InitMain() {
PORTB = 0; // set PORTB to 0
TRISB = 0; // designate PORTB pins as output

ADCON1 = 7; // all ADC pins to digital I/O


PORTA = 255;
TRISA = 255; // configure PORTA pins as input

PORTC = 0; // set PORTC to 0


TRISC = 0; // designate PORTC pins as output
PWM1_Init(5000); // Initialize PWM1 module at 5KHz
}

void main() {
initMain();

current_duty = 16; // initial value for current_duty


old_duty = 0; // old_duty will keep the 'old current_duty' value
PWM1_Start(); // start PWM1

while (1) { // endless loop


if (Button(&PORTA, 0,1,1)) // button on RA0 pressed
current_duty++ ; // increment current_duty
if (Button(&PORTA, 1,1,1)) // button on RA1 pressed

BY: Yeshaw A. Electro Mechanical Engineering Dpt. HU 2016 6


Students Practice Manual: Embedded Control System for EME Students
current_duty-- ; // decrement current_duty

if (old_duty != current_duty) { // if change in duty cycle requested


PWM1_Change_Duty(current_duty); // set new duty ratio,
old_duty = current_duty; // memorize it
PORTB = old_duty; // and display on PORTB
}
Delay_ms(200); // slow down change pace a little
}
}

/*
* Project name:
ADC_on_LEDs (Displaying ADC result on port LEDs WITH REFERENCE RA3)
*
*/

unsigned int temp_res;

void main() {
ADRESL=0X0C;
TRISA = 0xFF;
ADRESH=0; // Configure analog inputs and Vref

// PORTA is input
TRISD = 0x3F; // Pins RB7, RB6 are outputs
TRISC = 0;
ADCON1.F4 = 1 ; // PORTD is output

do {
temp_res = ADC_Read(2); // Get 10-bit results of AD conversion
PORTC = temp_res; // Send lower 8 bits to PORTD
PORTD = temp_res >> 2; // Send 2 most significant bits to RB7, RB6
} while(1);
}

/*
* Project name:
ADC_on_LEDs (Displaying ADC result on port LEDs)
* Copyright:
(c) Mikroelektronika, 2005-2008.

BY: Yeshaw A. Electro Mechanical Engineering Dpt. HU 2016 7


Students Practice Manual: Embedded Control System for EME Students
* Description:
The code demonstrates using library function for AD conversion. 10 bits
result is displayed on PORTB (two most significant bits) and PORTD. Notice
the changes on LEDs on portD while you you turn P1.

*/

unsigned int temp_res;

void main() {
ADCON1 = 0x80; // Configure analog inputs and Vref
TRISA = 0xFF; // PORTA is input
TRISD = 0x3F; // Pins RB7, RB6 are outputs
TRISB = 0; // PORTD is output

do {
temp_res = ADC_Read(2); // Get 10-bit results of AD conversion
PORTB = temp_res; // Send lower 8 bits to PORTD
PORTD = temp_res >> 2; // Send 2 most significant bits to RB7, RB6
} while(1);
}

/*
* Project name:
ADC_on_LEDs (Displaying ADC result on port LEDs)
* Copyright:
(c) Mikroelektronika, 2005-2008.
* Description:
The code demonstrates using library function for AD conversion. 10 bits
result is displayed on PORTB (two most significant bits) and PORTD. Notice
the changes on LEDs on portD while you you turn P1.
* Test configuration:
MCU: PIC16F877A
Dev.Board: EasyPIC4
Oscillator: HS, 08.0000 MHz
Ext. Modules: -
SW: mikroC v8.0.0.0
* NOTES:
On EasyPIC4 jumper should be on RA2 at A-D Converter Input.
*/

unsigned int temp_res;

BY: Yeshaw A. Electro Mechanical Engineering Dpt. HU 2016 8


Students Practice Manual: Embedded Control System for EME Students
void main() {
ADCON1 = 0x10; // Configure analog inputs and Vref
TRISA = 0xFF; // PORTA is input
TRISD = 0x3F; // Pins RB7, RB6 are outputs
TRISB = 0; // PORTD is output

do {
temp_res = ADC_Read(2); // Get 10-bit results of AD conversion
PORTB = temp_res; // Send lower 8 bits to PORTD
PORTD = temp_res >> 2; // Send 2 most significant bits to RB7, RB6
} while(1);
}

//ADC BY 10 VDD VSS REF


unsigned int temp_res;

void main() {
ADRESL=0X04;
TRISA = 0xFF;
ADRESH=0; // Configure analog inputs and Vref

// PORTA is input
TRISD = 0x3F; // Pins RB7, RB6 are outputs
TRISB = 0;
ADCON1.F4 = 0 ; // PORTD is output

do {
temp_res = ADC_Read(2); // Get 10-bit results of AD conversion
PORTB = temp_res; // Send lower 8 bits to PORTD
PORTD = temp_res >> 2; // Send 2 most significant bits to RB7, RB6
} while(1);
}

BY: Yeshaw A. Electro Mechanical Engineering Dpt. HU 2016 9


Students Practice Manual: Embedded Control System for EME Students

// 7 seg statics

unsigned short digit_no, digit1, digit2;

void main() {
//OPTION_REG = 0x80; // Timer0 settings
PORTD = 0; // clear PORTA (make sure both displays are off)
TRISD = 0; // designate PORTA pins as output
PORTC = 0; // clear PORTD (make sure LEDs are off)
TRISC = 0; // designate PORTD pins as output
//TMR0 = 0; // clear TMRO
digit1 = 0x7F; // mask for 8 (7 seg. display)
digit2 = 0x4F; // mask for 3 (7 seg display)
//INTCON = 0xA0; // Enable GIE, TMR0IE
// wait for interrupt
digit_no=0;
while(1)
{
if (digit_no==0) {
PORTD = 0;

PORTC= digit2; // send mask for digit 3 to PORTD


PORTD= 1;

digit_no = 1;
Delay_ms(5); // turn on 1st 7 seg., turn off 2nd
} else {
PORTD = 0;
PORTC = digit1; // send mask for digit 8 to PORTD
PORTD= 2; // turn on 2nd 7 seg., turn off 1st
digit_no = 0;
Delay_ms(10); // turn on 1st 7 seg., turn off 2nd
}
//Delay_ms(1000);
}
}

//PROGRAM CODE FOR SINGLE 7 SEGMENT

unsigned short i;
unsigned short mask(unsigned short num) {
switch (num) {
case 0 : return 0x3F;

BY: Yeshaw A. Electro Mechanical Engineering Dpt. HU 2016 10


Students Practice Manual: Embedded Control System for EME Students
case 1 : return 0x06;
case 2 : return 0x5B;
case 3 : return 0x4F;
case 4 : return 0x66;
case 5 : return 0x6D;
case 6 : return 0x7D;
case 7 : return 0x07;
case 8 : return 0x7F;
case 9 : return 0x6F;
} //case end
}
void main()
{

INTCON = 0; // Disable GIE, PEIE,INTE,RBIE,T0IE


TRISC=0X00;
PORTC=0X00;
TRISD=0X00;
PORTD=0X00;
do {
for (i = 0; i <= 9u; i++) {
PORTD = 0; // Turn off all 7seg displays
PORTC = mask(i); // bring appropriate value to PORTD
PORTD = 1; // turn on appropriate 7seg. display
Delay_ms(1000);
}
} while (1); //endless loop
}

//PROGRAM CODE FOR TWO 7 SEGMENT

unsigned short digit_no, digit10, digit1, digit, i;


void interrupt() {
if (digit_no==0) {
PORTD = 0; // Turn off all 7seg displays
PORTC = digit1; // send mask for ones digit to PORTD
PORTD = 1; // turn on 1st 7 seg., turn off 2nd
digit_no = 1;
}
else {
PORTD = 0; // Turn off all 7seg displays
PORTC = digit10; // send mask for tens digit to PORTD
PORTD = 2; // turn on 2nd 7 seg., turn off 1st
digit_no = 0;
}
TMR0 =0; // clear TMRO
INTCON = 0x20; // clear TMR0IF and set TMR0IE

BY: Yeshaw A. Electro Mechanical Engineering Dpt. HU 2016 11


Students Practice Manual: Embedded Control System for EME Students
}

unsigned short mask(unsigned short num) {


switch (num) {
case 0 : return 0x3F;
case 1 : return 0x06;
case 2 : return 0x5B;
case 3 : return 0x4F;
case 4 : return 0x66;
case 5 : return 0x6D;
case 6 : return 0x7D;
case 7 : return 0x07;
case 8 : return 0x7F;
case 9 : return 0x6F;
} //case end
}
void main()
{

OPTION_REG = 0x87; // Timer0 settings


TMR0 = 0; // clear TMRO
INTCON = 0xA0; // Disable PEIE,INTE,RBIE,T0IE
PORTD = 0; // clear PORTA (make sure both displays are off)
TRISD = 0; // designate PORTA pins as output
PORTC = 0; // clear PORTD (make sure LEDs are off)
TRISC = 0; // designate PORTD pins as output
do {
for (i = 0; i<=99; i++) { // count from 0 to 99
digit = i % 10u;
digit1 = mask(digit); // prepare ones digit
digit = (char)(i / 10u) % 10u;
digit10 = mask(digit); // prepare tens digit
Delay_ms(1000);
}
} while (1);
}

char *text = "MICROELECTRONCA";


void main()
{
TRISC=0X00;
PORTC=0X00;

BY: Yeshaw A. Electro Mechanical Engineering Dpt. HU 2016 12


Students Practice Manual: Embedded Control System for EME Students
Lcd_Config(&PORTC, 0, 1, 2, 7, 6, 5, 4);
Lcd_Init(&PORTC); // Lcd_Init_EP4, see Autocomplete
LCD_Cmd(LCD_CLEAR); // Clear display
LCD_Cmd(LCD_CURSOR_OFF); // Turn cursor off
Delay_ms(1000);
while(1)
{
LCD_Out(1,15, text); // Print text to LCD, 1st row, 1st column
Delay_ms(1000);
LCD_Out(2,6,"mikroE");
} // Print text to LCD, 2nd row, 6th column
}

//movable letters
char *text[5] = {'A','B','C','D'};
unsigned short int i;
void main()
{
TRISC=0X00;
PORTC=0X00;
Lcd_Config(&PORTC, 0, 1, 2, 7, 6, 5, 4);
Lcd_Init(&PORTC); // Lcd_Init_EP4, see Autocomplete
LCD_Cmd(LCD_CLEAR); // Clear display
LCD_Cmd(LCD_CURSOR_OFF); // Turn cursor off
Delay_ms(1000);
while(1)
{
for(i=0;i<=5;i++)
{

Lcd_Chr(1,i+1,text[i]); // Print text to LCD, 1st row, 1st column


Delay_ms(1000);
}
LCD_Cmd(LCD_CLEAR);
} // Print text to LCD, 2nd row, 6th column
}

//movable WITH STRING letters


char *text = " MICROELECTRNICA ";
unsigned short int i;
void main()
{
TRISC=0X00;

BY: Yeshaw A. Electro Mechanical Engineering Dpt. HU 2016 13


Students Practice Manual: Embedded Control System for EME Students
PORTC=0X00;
Lcd_Config(&PORTC, 0, 1, 2, 7, 6, 5, 4);
Lcd_Init(&PORTC); // Lcd_Init_EP4, see Autocomplete
LCD_Cmd(LCD_CLEAR); // Clear display
LCD_Cmd(LCD_CURSOR_OFF); // Turn cursor off
Delay_ms(1000);
while(1)
{
Lcd_Out(1,16,text);
//LCD_Cmd(LCD_SHIFT_LEFT); // Print text to LCD, 1st row, 1st column
for(i=0;i<=50;i++)
{
LCD_Cmd(LCD_SHIFT_LEFT);

Delay_ms(100);
}
LCD_Cmd(LCD_CLEAR);
}

// Print text to LCD, 2nd row, 6th column


}

BY: Yeshaw A. Electro Mechanical Engineering Dpt. HU 2016 14


Students Practice Manual: Embedded Control System for EME Students

SPI

//SPI PROGRAM
const unsigned short _CHIP_SELECT = 1;

unsigned short data = 0;


unsigned short recOK,buffer;

void main()
{

Soft_Spi_Config(&PORTC, 4, 5, 3);
TRISC &=~(_CHIP_SELECT);
TRISB=0X00;
PORTB=0X00;
while(1)
{
PORTC&=~(_CHIP_SELECT);
Delay_us(1);
Soft_Spi_Write(0XA3);
Delay_us(1);
PORTB=Soft_Spi_Read(0XA4);
Delay_us(1);
PORTC|= _CHIP_SELECT;
Delay_ms(1000);
}
// send data via UART
}

/* this programme code is used for timer0 as COUNTER */


unsigned int cnt;

void main()
{
OPTION_REG.F5=1;
OPTION_REG.F3=1;
//ADCON1=0X07;
//ADCON0=0X00;
TRISA=0XFF;
PORTA=0X00;
PORTB=0X00;
TRISB=0X00;
TMR0=0;
//INTCON=0XA0;

BY: Yeshaw A. Electro Mechanical Engineering Dpt. HU 2016 15


Students Practice Manual: Embedded Control System for EME Students
//cnt=0;
while (1)
{
if(TMR0==4)
{
PORTB.F1=1;

}
}

/* this programme code is used for timer0 as COUNTER */


unsigned int cnt;

void main()
{
OPTION_REG=0X30;
//ADCON1=0X07;
//ADCON0=0X00;
TRISA=0XFF;
PORTA=0X00;
PORTB=0X00;
TRISB=0X00;
TMR0=0;
//INTCON=0XA0;
//cnt=0;
while (1)
{
if(TMR0==4)
{
PORTB.F1=1;

}
}

unsigned int temp_res;

void main() {
//ADCON1 = 0x80;
ADRESL=0X0C; // Configure analog inputs and Vref
TRISA = 0xFF; // PORTA is input
TRISD = 0x3F; // Pins RB7, RB6 are outputs

BY: Yeshaw A. Electro Mechanical Engineering Dpt. HU 2016 16


Students Practice Manual: Embedded Control System for EME Students
TRISB = 0;
ADRESH=0X00; // PORTD is output
ADCON1.F4=1;
do {
temp_res = ADC_Read(2); // Get 10-bit results of AD conversion
PORTB= temp_res; // Send lower 8 bits to PORTD
PORTD = temp_res >> 2; // Send 2 most significant bits to RB7, RB6
} while(1);
}

7 SEG DECODER WITH SERIAL COMMUNICATION

/*
* Project name:
Spi_01 (Demonstration of SPI Library usage)
* Copyright:
(c) MikroElektronika, 2005-2008
* Description:
This code demonstrates using library routines for SPI communication.
Also, this example demonstrates working with MAX7219.
Eight 7 segment displays are connected to MAX7219. MAX7219 is connected to RC0
and SDO,SDI,SCKL pins are appropriatly connected.
* Test configuration:
MCU: PIC16F877A
Dev.Board: EasyPIC4
Oscillator: HS, 08.0000 MHz
Ext. Modules: mE Serial 7-seg Display2 board on PORTC
SW: mikroC v8.0.0.0
* NOTES:
None.
*/

#include "m7219.h"

extern const char CHIP_SEL;


char i, j;

void main() {

SPI_init(); // standard configuration


// instead of SPI_init, you can use SPI_init_Advanced as shown below

BY: Yeshaw A. Electro Mechanical Engineering Dpt. HU 2016 17


Students Practice Manual: Embedded Control System for EME Students
// SPI_init_Advanced(Master_OSC_div64, Data_SAMPLE_MIDDLE ,
CLK_Idle_HIGH , HIGH_2_LOW);
TRISC.CHIP_SEL = 0; // CS pin is defined in m7219.c file
max7219_init1(); // initialize max7219

while (1) {
for (j=0; j<10; j++)
for (i=8; i>0; i--) {
select_max(); // select max7219
Spi_Write(i); // digit No
Spi_Write(j); // digit Value
deselect_max(); // deselect max7219
Delay_ms(100);
}
}
}

BY: Yeshaw A. Electro Mechanical Engineering Dpt. HU 2016 18

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy