Important Codes
Important Codes
: 1 U20EE022
[A] Write an embedded C program to generate a square wave with frequency of 500 Hz
(i.e., T =2 msec).
CODE:
#include<reg51.h>
void time_delay(void);
void time1_delay(void);
void main() {
while(1){
P1=0xFF;
time_delay();
P1 = 0x00;
time1_delay();
}
}
void time_delay(void) {
TMOD=0x01;
TH0=0xFA;
TL0=0xA1; //to generate 1 ms delay for on time
TR0=1;
while(TF0==0){;}
TF0=0;
TR0=0;
}
void time1_delay(void){
TMOD=0x01;
TH0=0xFA;
TL0=0xA1; //to generate 1 ms delay for off time
TR0=1;
while(TF0==0){;}
TF0=0;
TR0=0;
}
OUTPUT:
CONCLUSION:
At the end of this practical, We learned different types of register bits of the timer to generate
square wave.
[A] Write an embedded C program to generate a square wave using timer 0 and mode 1.
CODE:
OUTPUT:
[B] Write an embedded C program to generate a PWM with a 20% duty cycle using timer
0 and mode 1.
CODE:
}
else
{
P1 = 0xFF;
TH0 = 0xFF;
TL0 = 0x77;
TR0 = 1;
}
OUTPUT:
[C] Write an embedded C program to generate a square wave using timer 0 and mode 2.
CODE:
#include <reg51.h>
void main()
{
P1 = 0xFF; //initializing port 1 with all high bits
TMOD = 0x02; //Timer 0 and Mode 2 selection
//mode2->Auto reload mode (any value of TH0 will give square wave here)
TH0 = 0x77;
TR0 = 1;
IE = 0x82;
while(1);
}
{
P1 = 0x00;
TR0 = 1;
}
else
{
P1 = 0xFF;
TR0 = 1;
}
OUTPUT:
CONCLUSION:
At the end of this practical, We learned different types of register bits of the timer as interrupts
and how to generate PWM with different duty cycles using timer mode 1 and 2.
[A] Write an embedded C program to generate a square wave with a variable duty cycle
and constant frequency of 500 Hz (i.e., T =2 msec).
CODE:
#include <reg51.h>
//i iteration
//j counter
//sbit PIN = P1^0;
short i = 0; //j=40 means 50e^-6 * 40 = 2 msec (10% duty of 20 msec)
short j = 40 ; //j= j+40 = 80 = 50e-6*80 = 4 msec (20% duty)
void main() //j increases till 360 (90% duty)
{
TMOD = 0x02; //mode2 -> auto-reload mode
TH0 = 0xD2; //to generate 50e-6 sec delay
IE = 0x82; //Interrupt enables
TR0 = 1; //timer starts
//PIN = 1;
P1 = 0xFF;
while(1);
}
OUTPUT:
[B] Write an embedded C program to generate a square wave with a constant duty cycle
(i.e.,10%) and variable frequency.
CODE:
#include <reg51.h>
//i iteration
//j counter
//sbit PIN = P1^0;
short i = 0; //j=400 means 50e^-6 * 400 = 20 msec period
short j = 400 ; //j= j+200 = 600 = 50e-6*600 = 30 msec period
void main() //j increases till 1200 (60 msec period)
{
TMOD = 0x02;
TH0 = 0xD2;
IE = 0x82;
TR0 = 1;
//PIN = 1;
P1 = 0xFF;
while(1);
}
if(i>j)
{
j = j+200;
i = 0;}}
if(j>1200)
{
j=400;
}
}
OUTPUT:
CONCLUSION:
At the end of this practical, We learned different types of register bits of the timer as interrupts
and how to generate a wave with variable duty and constant frequency and also with variable
frequency and constant duty.
[A] Generate PWM signal of 50% duty cycle for a microcontroller of clock frequency
(24.5 / 8)Mhz and time period 2 seconds using interrupt 1 timer 0 with prescaler as 48 on
port 5.0 as LED.
CODE:
#include<C8051F120.h>
void osc_init(void);
void port_init(void);
void timer0_init(void);
sbit pin = P5^0;
int count = 0;
void main(void)
{
WDTCN = 0XDE;
WDTCN = 0XAD; //Disabling watchdog timer
osc_init();
port_init();
timer0_init();
EA = 1;
while(1)
{
if(count == 1)
{
SFRPAGE = CONFIG_PAGE;
pin = ~pin;
count = 0;
SFRPAGE = TIMER01_PAGE;
Sys clk freq = (24.5/8) MHz, (Oscillator freq = sys clk freq / 48)
1 count = (1/oscillator freq) = 15.674 usec
On time = 1 sec for 1 sec 63799 count
65536 – 63799 1736 06C9h
TH0 = 0x06;
TL0 = 0xC9;
TR0 = 1;
}
}
}
void osc_init(void)
{
char SFRPAGE_SAVE = SFRPAGE;
SFRPAGE = CONFIG_PAGE;
void port_init(void)
{
char SFRPAGE_SAVE = SFRPAGE;
SFRPAGE = CONFIG_PAGE;
P5MDOUT = 0XFF; //push-pull configuration
//P5 = 0;
P5 = 0x00;
SFRPAGE = SFRPAGE_SAVE;
}
void timer0_init(void)
{
char SFRPAGE_SAVE = SFRPAGE;
SFRPAGE = TIMER01_PAGE;
TMOD = 0x01; //mode 1, 16 bit
CKCON = 0x20; //system clk freq / 48
TH0 = 0x06;
TL0 = 0xC9;
ET0 = 1;
TR0 = 1;
SFRPAGE = SFRPAGE_SAVE;
}
OUTPUT:
observed that the LED on the kit exhibited a recurring pattern of turning on for 1 second and
then turning off for 1 second. This consistent cycle of on-off transitions resulted in a total time
period of 2 seconds.
[B] Generate PWM signal of 40% duty cycle for a microcontroller of clock frequency (24.5
/ 8)Mhz and time period 1.5 seconds using interrupt 1 timer 0 with prescaler as 48 on port
5.1 as LED and observe the waveform in DSO.
CODE:
#include<C8051F120.h>
void osc_init(void);
void port_init(void);
void timer_init(void);
unsigned char count = 0;
sbit PIN = P5^1;
void main()
{
WDTCN = 0XDE;
WDTCN = 0XAD; //Disabling watchdog timer
osc_init();
port_init();
timer_init();
EA = 1; //Enabling interrupt
while(1)
{
if(count)
{
SFRPAGE = CONFIG_PAGE;
{
if(PIN ==0)
{
PIN = 1;
count = 0;
SFRPAGE = TIMER01_PAGE;
Sys clk freq = (24.5/8) MHz, (Oscillator freq = sys clk freq / 48)
1 count = (1/oscillator freq) = 15.674 usec
On time = 0.4*1.5 sec for 0.6 sec 38281 count
65536 – 38281 27255 6A77h
TH0 = 0X6A;
TL0 = 0X77;
TR0 = 1;
}
else
{
PIN = 0;
count = 0;
SFRPAGE = TIMER01_PAGE;
Sys clk freq = (24.5/8) MHz, (Oscillator freq = sys clk freq / 48)
1 count = (1/oscillator freq) = 15.674 usec
void osc_init()
{
char sfrsave = SFRPAGE;
SFRPAGE = CONFIG_PAGE;
OSCICN = 0X80; //(internal osc freq = 24.5) / 8
CLKSEL = 0X00; //select system clk freq only
SFRPAGE = sfrsave;
}
void timer_init()
{
char sfrsave = SFRPAGE;
SFRPAGE = TIMER01_PAGE;
TMOD =0X01; //Timer mode 1
CKCON = 0X02; // Prescaler = 48, fclk = sysclk/48
TH0 = 0X6A;
TL0 = 0X77;
ET0 = 1;
TR0 = 1;
SFRPAGE = sfrsave;
}
void port_init()
{
char sfrsave = SFRPAGE;
P5MDOUT = 0XFF; //Pushpull configuration
P5 = 0XFF;
SFRPAGE = sfrsave;
}
OUTPUT:
CONCLUSION:
We have successfully created square waves with different duties in C8051f12x and observed
that C8051f12x requires loading different SFR pages for loading different SFRs, also in
C8051f12x there are various prescalers for dividing Oscillator frequencies using CKCON,
CLKSEL registers.
[A] Write an Embedded ‘c’ program to generate a square pulse of 100 msec using
Timer-2, auto-reload mode.
CODE:
#include <C8051f120.h>
#define SYSCLK 24500000/8
#define TIMER_PRESCALER 12
#define LED_TOGGLE_RATE 50
#define TIMER_TICKS_PER_MS SYSCLK/TIMER_PRESCALER/1000
#define AUX1 LED_TOGGLE_RATE*TIMER_TICKS_PER_MS
#define AUX2 -AUX1
void port_init(void);
void timer2_init(void);
sfr16 RCAP = 0XCA; //Timer2 reload Register
sfr16 TMR2 = 0XCC; //Timer2 Register
void port_init(void)
{
char SFRPAGE_SAVE = SFRPAGE;
XBR2 = 0X40;
P1MDOUT = 0X40; //push-pull configuration
SFRPAGE = SFRPAGE_SAVE;
}
void timer2_init(void)
{
char SFRPAGE_SAVE = SFRPAGE;
SFRPAGE = TMR2_PAGE;
TMR2CF &= ~0X18; //SYSCLK/2
RCAP2 = AUX2;
TMR2 = RCAP2;
TMR2CN = 0X04;
ET2 = 1; //Enabling Timer-2
SFRPAGE = SFRPAGE_SAVE;
}
void main()
{
WDTCN = 0XDE; //Disabling watchdog timer
WDTCN = 0XAD;
timer2_init();
port_init();
EA = 1;
while(1);
}
OUTPUT:
CONCLUSION:
We have successfully created square waves with different duties in C8051f12x and observed
that C8051f12x requires loading different SFR pages for loading different SFRs, also in
C8051f12x there are various pre-scalers for dividing Oscillator frequencies using CKCON,
CLKSEL registers.
[A] Write an Embedded ‘c’ program to interface LCD display and print
“HELLO_SVNIT” on the first line and “ELECTRICAL” on the second line of the 16*2
display screen.
CODE:
#include <c8051f120.h>
sbit RS = P4^2;
sbit RW = P4^1;
sbit E = P4^0;
unsigned char msg1[] = {"HELLO_SVNIT"};
unsigned char msg2[] = {"ELECTRICAL"};
void OSCILLATOR_init(void);
void PORT_init(void);
void LCD_init(void);
void LCD_cmd(unsigned char);
void LCD_data(unsigned char);
void LCD_display(unsigned char);
void delay(void);
void main(void)
{
WDTCN = 0XDE;
WDTCN = 0XAD;
PORT_init();
OSCILLATOR_init();
LCD_init();
LCD_cmd(0X80);
LCD_display(1);
delay();
LCD_cmd(0XC0);
LCD_display(2);
while(1)
{
;
}
}
void OSCILLATOR_init(void)
{
void PORT_init(void)
{
char SFRPAGE_SAVE = SFRPAGE;
SFRPAGE = CONFIG_PAGE;
P7MDOUT = 0XFF;
P7 = 0X00;
P4MDOUT = 0XFF;
P4 = 0X00;
XBR0 = 0X37;
XBR1 = 0X7E;
XBR2 = 0X5C;
SFRPAGE = SFRPAGE_SAVE;
}
void LCD_init(void)
{
char SFRPAGE_SAVE = SFRPAGE;
SFRPAGE = CONFIG_PAGE;
SFRPAGE = SFRPAGE_SAVE;
}
if(x==1)
{
for(i=0;i<11;i++)
{
LCD_data(msg1[i]);
}
}
LCD_cmd(0XC0);
if(x==2)
{
for(i=0;i<10;i++)
{
LCD_data(msg2[i]);
}
}
SFRPAGE = SFRPAGE_SAVE;
}
void delay(void)
{
unsigned int i,j;
for(i=0;i<50;i++)
{
for(j=0;j<1000;j++)
{
;
}
}
}
OUTPUT:
CONCLUSION:
We have successfully printed messages on LCD display with C8051f12x and observed that
C8051f12x requires loading different SFR pages for loading different SFRs, in C8051f12x there
are various registers used to write a message in LCD display.
[A] Write an Embedded ‘c’ program to generate PWM signal on P0.0 of 8 KHz
frequency with 40% duty cycle using Programmable Counter Array(PCA) in 8-bit PWM
mode.
CODE:
#include <c8051f120.h>
void osc_init(void); //initializing oscillator
void port_init(void); //initializing ports
void pca0_init(void); //initializing PCA0
void main()
{
WDTCN = 0XDE;
WDTCN = 0XAD; //watchdog timer turned off and turned on
osc_init(); //initializing oscillator
port_init(); //Initializing ports
pca0_init(); //Initializing pca0
while(1);
}
void osc_init(void)
{
char sfrsave = SFRPAGE; //saving sfrpage
SFRPAGE = CONFIG_PAGE; //loading config page
OSCICN = 0X83; //sysclock frequency 24.5/1 MHZ
CLKSEL = 0X00; //using sys clk and divinding it by 1
SFRPAGE = sfrsave; //reloading saved sfrpage
}
void port_init(void)
{
char sfrsave = SFRPAGE; //saving sfrpage
SFRPAGE = CONFIG_PAGE; //loading config page
XBR0 = 0X08; //XBR PCA0 peripheral assignment
XBR1 = 0X00;
XBR2 = 0X40;
P0MDOUT = 0XFF; //port0 as output in pushpull mode
P0 = 0X00; //port 0 as output
SFRPAGE = sfrsave; //reloading saved sfrpage
}
void pca0_init(void)
{
OUTPUT:
CONCLUSION:
We have successfully created square waves with different duties using PCA registers in
C8051f12x and observed that C8051f12x requires loading different SFR pages for loading
different SFRs, also in C8051f12x there are various pre-scalers for dividing Oscillator
frequencies using CKCON, CLKSEL registers and also PCA registers like PCA0CN,
PCA0MDOUT.
[A] Write an Embedded ‘c’ program to generate sine and cosine waves using timer 4 and
display it to DAC0 and DAC1. Use 49 MHz as the system clock.
CODE:
void main(void);
void OSCILLATOR_Init(void);
void DAC0_Init (void);
void DAC1_Init (void);
void TIMER4_Init(int counts);
void Set_DACs(void);
//Turn on the PLL and increase the system clock by a factor of M/N = 2
SFRPAGE = CONFIG_PAGE;
PLL0CN = 0x00; // Set internal osc. as PLL source
SFRPAGE = LEGACY_PAGE;
FLSCL = 0x10; // Set FLASH read time for 50MHz clk or less
SFRPAGE = CONFIG_PAGE;
PLL0CN |= 0x01; // Enable Power to PLL
PLL0DIV = 0x01; // Set Pre-divide value to N (N = 1)
PLL0FLT = 0x01; // Set the PLL filter register for a reference clock
from 19 - 30 MHz and an output clock from 45 - 80 MHz
PLL0MUL = 0x02; // Multiply SYSCLK by M (M = 2)
for (i=0; i < 256; i++) ; // Wait at least 5us
PLL0CN |= 0x02; // Enable the PLL
while(!(PLL0CN & 0x10)); // Wait until PLL frequency is locked
CLKSEL = 0x02; // Select PLL as SYSCLK source
SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
}
void DAC0_Init(void){
char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = DAC0_PAGE;
DAC0CN = 0x94; // Enable DAC0 in left-justified mode
// managed by Timer4 overflows
SFRPAGE = LEGACY_PAGE;
REF0CN |= 0x03; // Enable the internal-VREF(2.4v) & Bias Generator
SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
}
void DAC1_Init(void){
void Set_DACs(void)
{
char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
static unsigned phase_acc = 0; // Holds phase accumulator
int SIN_temp, COS_temp; // Temporary 16-bit variables
unsigned char index; // Index into SINE table
phase_acc += PHASE_ADD; // Increment phase accumulator
index = phase_acc >> 8;
SIN_temp = SINE_TABLE[index]; // Read the table value
index += 64; // 90 degree phase shift
COS_temp = SINE_TABLE[index];
SFRPAGE = DAC0_PAGE;
DAC0 = 0x8000 ^ SIN_temp; // Write to DAC0
SFRPAGE = DAC1_PAGE;
DAC1 = 0x8000 ^ COS_temp; // Write to DAC1
SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
}
OUTPUT:
CONCLUSION:
In summary, the experiment successfully generated sine and cosine waves using Timer 4 on the
C8051f12x microcontroller. Key registers including DAC0CN, DAC1CN, and REF0CN were
configured to enable DAC functionality. Additionally, the PLL was utilized to achieve the
specified system clock frequency, with registers PLL0CN, FLSCL, PLL0DIV, PLL0FLT, and
PLL0MUL being appropriately configured to ensure accurate waveform generation.
[A] Write an Embedded ‘c’ program to convert analog value to digital value and display
it into LCD display using timer 3 and PLL.
CODE:
#include <c8051f120.h>
sfr16 ADC0_Data = 0xbe;
sfr16 RCAP3 = 0xca;
sfr16 TMR3 = 0xcc;
bit adc_flag;
bit timer0_flag;
void OSCILLATOR_Init(void);
void PORT_Init(void);
void ADC0_Init(void);
void TIMER3_Init(int counts);
void ADC0_ISR(void);
void send_lcd_data(void);
void lcd_initialize(void);
void lcd_command(unsigned char);
void lcd_data(unsigned char);
void lcd_busy(void);
void lcd_display(void);
void Timer0_Init(void);
unsigned int result;
unsigned char disp_data[5];
void OSCILLATOR_Init(void)
{
int loop;
char SFRPAGE_SAVE = SFRPAGE;
SFRPAGE = CONFIG_PAGE;
OSCICN = 0x83;
CLKSEL = 0x00;
SFRPAGE = CONFIG_PAGE;
PLL0CN = 0x00;
SFRPAGE = LEGACY_PAGE;
FLSCL = 0x10;
SFRPAGE = CONFIG_PAGE;
PLL0CN |= 0x01;
PLL0DIV = 0x01;
PLL0FLT = 0x01;
PLL0MUL = 0x02;
for (loop=0; loop < 256; loop++);
PLL0CN |= 0x02;
while(!(PLL0CN & 0x10));
CLKSEL = 0x02;
SFRPAGE = SFRPAGE_SAVE;
}
void Timer0_Init(void)
{
char SFRPAGE_SAVE = SFRPAGE;
SFRPAGE = TIMER01_PAGE;
TH0 = 0x3c;
TL0= 0xb0 ;
TMOD = 0x01;
CKCON = 0x02; // 1:48 prescaler
ET0 = 1;
TR0=1 ;
SFRPAGE = SFRPAGE_SAVE;
}
send_lcd_data (void)
{
int i;
char SFRPAGE_SAVE = SFRPAGE;
SFRPAGE = CONFIG_PAGE;
//lcd_command (0x8C);
for(i=0; i<5; i++)
{
lcd_data(disp_data[i]);
// Save Current SFR page
}
lcd_command (0x02);
SFRPAGE = SFRPAGE_SAVE;
return;
}
// subroutine to check LCD busy or not before sending new data or command
void lcd_busy (void)
{
for(ia=0;ia<6500;ia++)
{
;
}
}
OUTPUT:
CONCLUSION:
In summary, the experiment successfully converted analog data of voltage to digital data and
displayed on LCD screen using Timer 3 on the C8051f12x microcontroller. Key registers
including ADC0CN, ADC0CF, and REF0CN were configured to enable ADC functionality.
Additionally, the PLL was utilized to achieve the specified system clock frequency, with
registers PLL0CN, FLSCL, PLL0DIV, PLL0FLT, and PLL0MUL being appropriately
configured to ensure accurate waveform generation.