Microcontroller For Embedded Systems RIC-651
Microcontroller For Embedded Systems RIC-651
Microcontroller For Embedded Systems RIC-651
1. Keil µVision:
The µVision IDE combines project management, run-time environment, build facilities, source
code editing, and program debugging in a single powerful environment. µVision is easy-to-use
and accelerates your embedded software development. µVision supports multiple screens and
allows you to create individual window layouts anywhere on the visual surface.
For learning purposes, you can try the evaluation version of Keil which has code limitation of 2K
bytes. You must download the C51 version for programming on 8051 microcontroller
architecture.
Step 2: To initiate the programming you must create a project using the keil Uvision IDE
The option to create a new project will be available under the project tab in the toolbar. Next,
you have to store the project in a folder and give a suitable name to it.
The device selection dialog provides you with the option to select the 8051 derivatives for
which you want to develop the program. If you are not sure about your device you can refer to
the description of the devices which is displayed on the left pane of the dialog. Accordingly,
select your device and click OK to confirm.
You must add C file to your project before you begin coding. This can be done by right-clicking
on your project from the project pane and selection “Add a new item to source group 1”. In the
next dialog box, you will be given with the choice on what type of file you want to add such as
C, C++, header, text etc. Since here we are dealing with Embedded C programming, select C File
(.c) option. Provide the necessary name, location and click on add.
Step 5: Coding in C
The main part has finally arrived, so now you can go along with programming in C with your
respective microcontroller.
Example: Blinking of LED connected to Port 1 of 8051(or any other Intel family controller like
Atmel controller in laboratory).
#include <reg51.h>
//delay function declaration
void delay(void);
void main(void)
{
//an infinite loop
while(1)
{
// Turn ON all LED's connected to Port1
P1 = 0xFF;
delay();
// Turn OFF all LED's connected to Port1
P1 = 0x00;
delay();
}
}
//delay function definition
void delay(void)
{
int i,j;
for(i=0;i<0xff;i++)
for(j=0;j<0xff;j++);
}
Step 6 : Compiling and building the C project using Keil Uvision IDE
In order to build recently created C program go to Project tab and click on Build Target on the
menu bar. An alternate way to do this is by pressing the F7 key. If the code that you have
written is correct, the code will successfully compile without any errors. You can check your
output in the Build Output pane.
Step 7: Generating the hex file using Keil Uvision IDE
The code you compiled cannot be directly fed to the microcontroller, it is not possible. For that
purpose, we have to generate the hex code for your respective file.
In order to generate the hex code, right click on the ‘Target 1’ folder and select options for
target ‘Target 1’. Select the Output tab in the target ‘Target 1’ dialog box. Make sure Create Hex
File option is checked and the HEX format should be HEX-80. Click OK.
Again rebuild your project by pressing F7. Your required hex file would have been generated
with the same as your project in the Objects folder.
If you wish you can also view your hex code by using a notepad.
In order to burn the hex code to your microcontroller, there are two ways which are specific to
the device you are working with. Some devices, for example, P89V51 they have their own built-
in bootloader and you can burn the hex code directly through the serial port. Mostly you will
require a specific programmer for your 8051 microcontroller through which you can easily
upload your hex code by connecting the programmer via normal USB port.
2. Flash Magic
Flash Magic is a PC tool for programming flash based microcontrollers from NXP using a serial
or Ethernet protocol while in the target hardware.
for(;;)
{
P2=0x55;
for( x=0;x<400;x++);
{
}
P2=0xAA;
for( x=0;x<400;x++);
{
}
}
}
OUTPUT- After the execution of the program the Led start blinking alternatively at odd and
even positions, after some delay.
EXPERIMENT 2
AIM: Write a program to generate a 10KHz square wave using 8051.
OUTCOME: Students will learn the fundamentals related to calculations for waveform
generations of different shapes and duty cycles.
HARDWARE USED: Microcontroller kit, Power supply, connecting wires.
SOFTWARE USED: Keil μvision4, Flash magic
// The Keil will be used. Since the waveform generation can be through compiler the flash
magic need not to be used.
Entire port or just a particular pin can be used to generate the waveforms.
PROGRAM:
#include<reg51.h>
void main()
{
unsigned int x;
for(;;)
{
P1=0X80;
for(x=0;x<40000;x++)
{
P1=0X00;
}
}
}
OUTPUT:
//Function Declaration
port_init();
InitINT0();
InitINT1();
//Main Function
main()
{
port_init();
InitINT0();
InitINT1();
while(1)
}
port_Init()
{
P0=0X00;
P1=0X00;
P2=0X00;
P3=0X0C;
}
InitINT0() //int0 ISR
{
IT0=1;
EX0=1;
EA=1;
}
InitINT1()//INT1 ISR
{
IT1=1;
EX1=1;
EA=1;
}
external0_isr()interrupt 0
{
LED1 = ~LED1;
}
external1_isr()interrupt 2
{
LED2 = ~LED2;
}
OUTPUT:
The interrupts given at PORT 3 (P2.2 & P2.3) using PULL-UP KEYS (K5 & K6) have been studied
successfully:-
When K5 pressed - LED on.
When K5 pressed again - LED off.
Similarly,
When K6 pressed - LED on.
When K6 pressed again - LED off
EXPERIMENT 4
OBJECTIVE: Write a program to display temperature using internal sensor of MSP430
and display the same in CCS.
OUTCOME: Students will learn using Temperature sensor and ADC in MSP430 and
defining interrupts on MSP430.
SOFTWARE USED: CCS (Code Composer Studio)
HARDWARE USED: MSP430F5529 LaunchPad
// The MSP430 board has internal temperature sensor and to use it ADC and Interrupts need to
programmed. Also there are some calibrations calculations required for which the manuals
have to be referred.
PROGRAM:
#include <msp430.h>
#define CALADC12_15V_30C *((unsigned int *)0x1A1A) // Temperature Sensor Calibration-30 C
//See device datasheet for TLV table memory mapping
#define CALADC12_15V_85C *((unsigned int *)0x1A1C) // Temperature Sensor Calibration-
85 C
int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
REFCTL0 &= ~REFMSTR; // Reset REFMSTR to hand over control to
ADC12_A ref control registers
ADC12CTL0 = ADC12SHT0_8 + ADC12REFON + ADC12ON;
Internal ref = 1.5V
ADC12CTL1 = ADC12SHP; // enable sample timer
ADC12MCTL0 = ADC12SREF_1 + ADC12INCH_10; // ADC i/p ch A10 = temp sense i/p
ADC12IE = 0x001; // ADC_IFG upon conv result-ADCMEMO __delay_cycles(100); // delay to
allow Ref to settle
ADC12CTL0 |= ADC12ENC;
while(1)
{
ADC12CTL0 &= ~ADC12SC; ADC12CTL0 |= ADC12SC;
// Sampling and conversion start
__bis_SR_register(LPM0_bits + GIE); // LPM0 with interrupts enabled
__no_operation();
Temperature in Celsius. See the Device Descriptor Table section in the System Resets,
Interrupts, and Operating Modes, System Control Module chapter in the device user's guide for
background information on the used formula.
//Temperature in Fahrenheit
Tf = (9/5)*Tc + 32 temperatureDegF = temperatureDegC * 9.0f / 5.0f + 32.0f;
}
}
#pragma vector=ADC12_VECTOR
__interrupt void ADC12ISR (void)
{
switch(__even_in_range(ADC12IV,34))
{
case 0: break; // Vector 0: No interrupt
case 2: break; // Vector 2: ADC overflow
case 4: break; // Vector 4: ADC timing overflow
case 6: // Vector 6: ADC12IFG0
temp = ADC12MEM0; // Move results, IFG is cleared
__bic_SR_register_on_exit(LPM0_bits); // Exit active CPU
break;
case 8: break; // Vector 8: ADC12IFG1
case 10: break; // Vector 10: ADC12IFG2
case 12: break; // Vector 12: ADC12IFG3
case 14: break; // Vector 14: ADC12IFG4
case 16: break; // Vector 16: ADC12IFG5
case 18: break; // Vector 18: ADC12IFG6
case 20: break; // Vector 20: ADC12IFG7
case 22: break; // Vector 22: ADC12IFG8
case 24: break; // Vector 24: ADC12IFG9
case 26: break; // Vector 26: ADC12IFG10
case 28: break; // Vector 28: ADC12IFG11
case 30: break; // Vector 30: ADC12IFG12
case 32: break; // Vector 32: ADC12IFG13
case 34: break; // Vector 34: ADC12IFG14
default: break;
}
}
EXPERIMENT 5
AIM: Generation of ramp waveform using DAC and 8051 microcontroller.
OUTCOME: Students will be able to learn using DAC in 8051 family available of
microcomputers available.
SOFTWARE USED: Keil μvision4, Flash magic
HARDWARE USED: Micro controller kit 8051, connecting wires and power supply
// The DAC is responsible for visualization of waveforms as the programming is done with
digital values but the waveform is constructed using predefined formulae considering the
applied supply.
Ex: For generation of Sinusoidal waveform the table has to be prepared for all possible angles.
The smallest the angles , the smoothen will be the waveform.
PROGRAM:
#include<reg51.h>
void main()
{unsigned int i;
while(1)
{for(i=0;i<256;i++)
{
P2=i;
}
}
}
OUTPUT:
Ramp Waveform
EXPERIMENT 6(A)
OBJECTIVE: To interface GPIO ports with pushbutton and Leds.
OUTCOME: Students will be able to learn configuring on-board switches and leds on
MSP430 Boards.
HARDWARE USED: MSP430F5529 LaunchPad
SOFTWARE USED: CCS
PROGRAM:
#include <msp430.h>
void main(void)
{
unsigned int i=0;
WDTCTL = WDTPW | WDTHOLD;
P1DIR |= 0x01; // Led is attached to P1.0
P4DIR |= 0x80;// Led is attched to P1.7
for(;;)
{
P1OUT ^= 0x01; //Toggle led p1.0
P4OUT ^= 0x80; //Toggle led p1.7
for(i=0;i<20000;i++)
{}
}
return 0;
}
EXPERIMENT 6(B)
PUSH BUTTON_LED
#include <msp430.h>
void main( void )
{
unsigned int i=0;
WDTCTL = WDTPW | WDTHOLD;
SW1 -P1.1, SW2- P2.1, LED1- P1.0, LED2- P4.7 P1REN |= 0x02; // Enable resistor on P1.1 P1OUT
= 0x03; //Set resistor to pull-up, P1.0 high P1DIR = 0x01; //P1.0 as output & P1.1 as intput
while(1)
{
if(!(P1IN & 0x02)) // If push button is pressed
{
P4OUT ^= 0x80;
}
if(!(P2IN & 0x02))
{
P1OUT ^= 0x01;
}
for(i=0;i<20000;i++)
{}
}
}
EXPERIMENT 7
OBJECTIVE: To Interface Potentiometer with GPIO.
OUTCOME: Students will be able to learn using analog sensors with MSP430 boards
and to display their values using serial monitor application available in
Energia.
HARDWARE USED: MSP-EXP430G2 LaunchPad, 10-kilohm Potentiometer, hook-up wire.
SOFTWARE USED: ENERGIA
// ANALOGREADSERIAL: Reads an analog input on pin A3, prints the result to the serial monitor.
Attach the center pin of a potentiometer to pin A3, and the outside pins to ~3V and ground.
void loop()
{
read the input on analog pin A3:
int sensorValue = analogRead(A3);
print out the value you read.
Serial.println(sensorValue);
delay(1); // delay in between reads for stability
}
void setup() {
//initialize serial communications at 9600 bps
Serial.begin(9600);
}
void loop() {
// read the analog in value
sensorValue = analogRead(analogInPin);
//wait 10 milliseconds before the next loop for the analog-to-digital converter to settle after
the last reading
delay(10);
}
OUTPUT:
The output will be intensity variations in on-board leds. Or if the variation is not seen easily, one
can program another led connected to any GPIO pin to visualize the impact of potentiometer.
EXPERIMENT 9
AIM: Write a program for PWM generation for MSP430 using Timers.
OUTCOME: Students will be able to learn to use timers for PWM generation.
SOFTWARE USED: Code Composer Studio and Tera term.
HARDWARE USED: Micro controller board MSP430, connecting wires ,power supply
WORKING OF SOFTWARE:
Tera Term (alternatively TeraTerm) is an open-source, free, software implemented, terminal
emulator (communications) program. It emulates different types of computer terminals, from
DEC VT100 to DEC VT382. It supports telnet, SSH 1 & 2 and serial port connections.
TeraTerm (as all terminal programs) connects to a COM port on your PC. It may be a real COM
port, or a virtual one such as the application UART of the LaunchPad, or the virtual COM port of
an USB/serial converter.
If using a real COM port or an USB/serial converter, you'll need to connect the MSP on the
launchPad correctly (including a level shifter).
If using the application UART, you have to set the jumpers properly for RX/TX. The maximum
baudrate is 9600 then (with the other two options, you can go up to 115200Bd or even higher).
PROGRAM:
#include <msp430f5529.h>
void main(void)
{
WDTCTL = WDTPW + WDTHOLD;
// Stop WDT
/*** GPIO Set-Up ***/
P1DIR |= BIT2; //
P1SEL |= BIT2; //SELECTION OF PERIPHERAL
P1DIR |= BIT3;
P1SEL |= BIT3;
//*** Timer0_A Set-Up ***/
TA0CCR0 |= 200 - 1;
TA0CCTL1 |= OUTMOD_7;
TA0CCR1 |= 100;
TA0CTL |= TASSEL_2 + MC_1;
//*** Timer1_A Set-Up ***/
TA0CCR0 |= 1000 - 1;
TA0CCTL2 |= OUTMOD_7;
TA0CCR2 |= 500;
TA0CTL |= TASSEL_2 + MC_1;
return 0;
}
OUTPUT:
EXPERIMENT 10
OBJECTIVE: Setting up CC3100 as an HTTP server.
OUTCOME: Students will be able to create a server and control the onboard leds
through it. They will also be able to program in HTML for creation of the
page.
HARDWARE USED: CC3100
SOFTWARE USED: ENERGIA
#include <WiFi.h>
void setup() {
Serial.begin(9600); // initialize serial communication
pinMode(RED_LED, OUTPUT); // set the LED pin mode attempt to connect to Wifi network
Serial.print("Attempting to connect to Network named: ");
//if the current line is blank, you got two newline characters in a row. That's the end of the
client HTTP request, so send a response.
if (strlen(buffer) == 0)
{
//HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK) and a content-type so
the client knows what's coming, then a blank line
client.println("HTTP/1.1 200 OK"); client.println("Content-type:text/html");
client.println();
void printWifiStatus()
{
// print the SSID of the network you're attached to.
Serial.print("SSID: ");
Serial.println(WiFi.SSID());