KEMBAR78
Important Codes | PDF | Microcontroller | Hertz
0% found this document useful (0 votes)
22 views33 pages

Important Codes

Cgffgffdd

Uploaded by

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

Important Codes

Cgffgffdd

Uploaded by

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

28/07/2023 Practical No.

: 1 U20EE022

Software: Keil uVision5 Microcontroller: 8051 {P89v51RD2}

[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.

Microprocessor and Microcontroller Bhumi Vavadiya


04/08/2023 Practical No.: 2 U20EE022

Software: Keil uVision5 Microcontroller: 8051 {P89v51RD2}

[A] Write an embedded C program to generate a square wave using timer 0 and mode 1.

CODE:

//Write an embedded C program to generate a square wave using timer 0


and mode 1.
#include <reg51.h>
void main()
{
P1 = 0xFF; //initializing port 1 with all high bits
TMOD = 0x01; //Timer 0 and Mode 1 selection
//consider freq = 10 KHz
//to set duty cycle 50% (i.e, on time = 50 usec)
//delay = (65536-x)*(12/33e6)
//if delay(on time = 50e-6)
//then x = FF77, TH0 = FF, TL0 = 77
TH0 = 0xFF;
TL0 = 0x77;
TR0 = 1; // timer start
IE = 0x82; //Interrupt Enable register set

while(1); //infinite loop


}

void T0ISR(void) interrupt 1


{
TR0 = 0;
if(P1 == 0xFF)
{
P1 = 0x00;
TH0 = 0xFF;
TL0 = 0x77;
TR0 = 1;
}
else
{
P1 = 0xFF;
TH0 = 0xFF;
TL0 = 0x77;
TR0 = 1;
}

Microprocessor and Microcontroller Bhumi Vavadiya


04/08/2023 Practical No.: 2 U20EE022

OUTPUT:

[B] Write an embedded C program to generate a PWM with a 20% duty cycle using timer
0 and mode 1.

CODE:

//Write an embedded C program to generate a PWM with a 20% duty cycle


using timer 0 and mode 1.
#include <reg51.h>
void main()
{
P1 = 0xFF; //initializing port 1 with all high
TMOD = 0x01; //Timer 0 and Mode 1 selection
//consider freq = 10 KHz
//to set duty cycle 20% (i.e, on time = 20 usec)
//delay = (65536-x)*(12/33e6)
//if delay(on time = 20e-6)
//then x = FF77, TH0 = FF, TL0 = C9
TH0 = 0xFF;
TL0 = 0xC9;
TR0 = 1; // timer start
IE = 0x82; //Interrupt Enable register set

while(1); //infinite loop


}

void T0ISR(void) interrupt 1


{
TR0 = 0;
if(P1 == 0xFF)
{ //off time calculation
P1 = 0x00; //by putting delay = 80 usec
TH0 = 0xFF;
TL0 = 0x24;
TR0 = 1;

Microprocessor and Microcontroller Bhumi Vavadiya


04/08/2023 Practical No.: 2 U20EE022

}
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:

//Write an embedded C program to generate a square wave using timer 0


and mode 2.

#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);
}

void T0ISR(void) interrupt 1


{
TR0 = 0;
if(P1 == 0xFF)

Microprocessor and Microcontroller Bhumi Vavadiya


04/08/2023 Practical No.: 2 U20EE022

{
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.

Microprocessor and Microcontroller Bhumi Vavadiya


11/08/2023 Practical No.: 3 U20EE022

Software: Keil uVision5 Microcontroller: 8051 {P89v51RD2}

[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);
}

void T0ISR(void) interrupt 1


{
if (i<j)
{ //PIN = 1;
P1 = 0xFF;
i++;
}
else
{
//PIN = 0;
P1 = 0x00;
i++;
if(i>400)
{
j = j+40;
i = 0;}}
if(j>360)
{
j=40;
}
}

Microprocessor and Microcontroller Bhumi Vavadiya


11/08/2023 Practical No.: 3 U20EE022

OUTPUT:

20% 30% 40% 50% 60% 70% 80% 90%


10%

[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);
}

void T0ISR(void) interrupt 1


{
if (i<40)
{ //PIN = 1; //i iterate till 40 * 50e-6 = 2 msec
P1 = 0xFF; //generation of 10% constant duty
i++;
}
else
{
//PIN = 0;
P1 = 0x00;
i++;

Microprocessor and Microcontroller Bhumi Vavadiya


11/08/2023 Practical No.: 3 U20EE022

if(i>j)
{
j = j+200;
i = 0;}}
if(j>1200)
{
j=400;
}
}

OUTPUT:

20ms 30ms 40ms 50ms 60ms 30ms


20ms 40ms 50ms 60ms

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.

Microprocessor and Microcontroller Bhumi Vavadiya


25/08 -01/09 Practical No.: 4 U20EE022
2023

Software: SI LABS IDE Microcontroller: C8051f12x

[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;

Microprocessor and Microcontroller Bhumi Vavadiya


25/08 -01/09 Practical No.: 4 U20EE022
2023

OSCICN = 0X80; //(internal osc freq = 24.5) / 8


CLKSEL = 0x00; //select system clk freq only
SFRPAGE = SFRPAGE_SAVE;
}

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;
}

void timer0_isr(void) interrupt 1


{
count++;
}

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.

Microprocessor and Microcontroller Bhumi Vavadiya


25/08 -01/09 Practical No.: 4 U20EE022
2023

[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

Microprocessor and Microcontroller Bhumi Vavadiya


25/08 -01/09 Practical No.: 4 U20EE022
2023

On time = 0.6*1.5 sec  for 0.9 sec  57421 count


65536 – 57421  8115  1FB3h
TH0 = 0X1F;
TL0 = 0XB3;
TR0 = 1;
}
}
}
}
}

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;
}

void timer0_tointerrupt(void) interrupt 1


{
count ++;
}

Microprocessor and Microcontroller Bhumi Vavadiya


25/08 -01/09 Practical No.: 4 U20EE022
2023

OUTPUT:

Time period = 1.48 sec


≈ 1.5 sec

Off time = 880 msec


= 0.88 sec
≈ 0.9 sec
(0.6*1.5 = 0.9 sec)

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.

Microprocessor and Microcontroller Bhumi Vavadiya


15/09/2023 Practical No.: 5 U20EE022

Software: SI LABS IDE Microcontroller: C8051f12x

[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

sbit LED = P1^6;

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 TIMER2_ISR(void) interrupt 5


{
LED = ~LED;
TF2 = 0;

Microprocessor and Microcontroller Bhumi Vavadiya


15/09/2023 Practical No.: 5 U20EE022

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.

Microprocessor and Microcontroller Bhumi Vavadiya


06/10/2023 Practical No.: 6 U20EE022

Software: SI LABS IDE Microcontroller: C8051f12x

[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)
{

Microprocessor and Microcontroller Bhumi Vavadiya


06/10/2023 Practical No.: 6 U20EE022

char SFRPAGE_SAVE = SFRPAGE;


SFRPAGE = CONFIG_PAGE;
OSCICN |= 0X03;
SFRPAGE = SFRPAGE_SAVE;
}

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;

LCD_cmd(0X01); //clear display


LCD_cmd(0X3C); //2nd line, 5*7 resolution
LCD_cmd(0X0F); //display on, cursor blinking
LCD_cmd(0X02); //return to home
LCD_cmd(0X06); //right_shift mode

SFRPAGE = SFRPAGE_SAVE;
}

void LCD_cmd(unsigned char temp)


{
delay();
RS = 0;
RW = 0;
P7 = temp;
E = 1;
E = 0;
}

Microprocessor and Microcontroller Bhumi Vavadiya


06/10/2023 Practical No.: 6 U20EE022

void LCD_data(unsigned char temp)


{
delay();
RS = 1;
RW = 0;
P7 = temp;
E = 1;
E = 0;
}

void LCD_display(unsigned char x)


{
unsigned char i;
char SFRPAGE_SAVE = SFRPAGE;
SFRPAGE = CONFIG_PAGE;

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++)
{
;
}
}
}

Microprocessor and Microcontroller Bhumi Vavadiya


06/10/2023 Practical No.: 6 U20EE022

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.

Microprocessor and Microcontroller Bhumi Vavadiya


13/10/2023 Practical No.: 7 U20EE022

Software: SI LABS IDE Microcontroller: C8051f12x

[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)
{

Microprocessor and Microcontroller Bhumi Vavadiya


13/10/2023 Practical No.: 7 U20EE022

char sfrsave = SFRPAGE; //saving sfrpage


SFRPAGE = PCA0_PAGE; //loading pca0 page
PCA0CN = 0X00; //making all the flag zero
PCA0MD = 0X00; //sysclk/12
PCA0CPM0 = 0X42; //01000010//8-bit PWM mode
PCA0CPH0 = (256 - (256*0.4));
CR = 1;
SFRPAGE = sfrsave; //reloading saved sfrpage
}

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.

Microprocessor and Microcontroller Bhumi Vavadiya


20/10/2023 Practical No.: 8 U20EE022

Software: SI LABS IDE Microcontroller: C8051f12x

[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:

#include <c8051f120.h> // SFR declarations

sfr16 RCAP4 = 0xca; // Timer4 capture/reload


sfr16 TMR4 = 0xcc; // Timer4
sfr16 DAC0 = 0xd2; // DAC0 data
sfr16 DAC1 = 0xd2; // DAC1 data

#define INTCLK 24500000 // Internal oscillator frequency in Hz


#define SYSCLK 49000000 // Output of PLL derived from (INTCLK*2)
#define SAMPLE_RATE_DAC 100000L // DAC sampling rate in Hz
#define PHASE_PRECISION 65536 // range of phase accumulator
#define FREQUENCY 1000 // Frequency of output waveform in Hz

// <PHASE_ADD> is the change in phase between DAC samples; It is used in


// the set_DACs routine.
unsigned int PHASE_ADD = FREQUENCY * PHASE_PRECISION / SAMPLE_RATE_DAC;

int code SINE_TABLE[256] = {

0x0000, 0x0324, 0x0647, 0x096a, 0x0c8b, 0x0fab, 0x12c8, 0x15e2,


0x18f8, 0x1c0b, 0x1f19, 0x2223, 0x2528, 0x2826, 0x2b1f, 0x2e11,
0x30fb, 0x33de, 0x36ba, 0x398c, 0x3c56, 0x3f17, 0x41ce, 0x447a,
0x471c, 0x49b4, 0x4c3f, 0x4ebf, 0x5133, 0x539b, 0x55f5, 0x5842,
0x5a82, 0x5cb4, 0x5ed7, 0x60ec, 0x62f2, 0x64e8, 0x66cf, 0x68a6,
0x6a6d, 0x6c24, 0x6dca, 0x6f5f, 0x70e2, 0x7255, 0x73b5, 0x7504,
0x7641, 0x776c, 0x7884, 0x798a, 0x7a7d, 0x7b5d, 0x7c29, 0x7ce3,
0x7d8a, 0x7e1d, 0x7e9d, 0x7f09, 0x7f62, 0x7fa7, 0x7fd8, 0x7ff6,
0x7fff, 0x7ff6, 0x7fd8, 0x7fa7, 0x7f62, 0x7f09, 0x7e9d, 0x7e1d,
0x7d8a, 0x7ce3, 0x7c29, 0x7b5d, 0x7a7d, 0x798a, 0x7884, 0x776c,
0x7641, 0x7504, 0x73b5, 0x7255, 0x70e2, 0x6f5f, 0x6dca, 0x6c24,
0x6a6d, 0x68a6, 0x66cf, 0x64e8, 0x62f2, 0x60ec, 0x5ed7, 0x5cb4,
0x5a82, 0x5842, 0x55f5, 0x539b, 0x5133, 0x4ebf, 0x4c3f, 0x49b4,
0x471c, 0x447a, 0x41ce, 0x3f17, 0x3c56, 0x398c, 0x36ba, 0x33de,
0x30fb, 0x2e11, 0x2b1f, 0x2826, 0x2528, 0x2223, 0x1f19, 0x1c0b,
0x18f8, 0x15e2, 0x12c8, 0x0fab, 0x0c8b, 0x096a, 0x0647, 0x0324,

0x0000, 0xfcdc, 0xf9b9, 0xf696, 0xf375, 0xf055, 0xed38, 0xea1e,


0xe708, 0xe3f5, 0xe0e7, 0xdddd, 0xdad8, 0xd7da, 0xd4e1, 0xd1ef,
0xcf05, 0xcc22, 0xc946, 0xc674, 0xc3aa, 0xc0e9, 0xbe32, 0xbb86,

Microprocessor and Microcontroller Bhumi Vavadiya


20/10/2023 Practical No.: 8 U20EE022

0xb8e4, 0xb64c, 0xb3c1, 0xb141, 0xaecd, 0xac65, 0xaa0b, 0xa7be,


0xa57e, 0xa34c, 0xa129, 0x9f14, 0x9d0e, 0x9b18, 0x9931, 0x975a,
0x9593, 0x93dc, 0x9236, 0x90a1, 0x8f1e, 0x8dab, 0x8c4b, 0x8afc,
0x89bf, 0x8894, 0x877c, 0x8676, 0x8583, 0x84a3, 0x83d7, 0x831d,
0x8276, 0x81e3, 0x8163, 0x80f7, 0x809e, 0x8059, 0x8028, 0x800a,
0x8000, 0x800a, 0x8028, 0x8059, 0x809e, 0x80f7, 0x8163, 0x81e3,
0x8276, 0x831d, 0x83d7, 0x84a3, 0x8583, 0x8676, 0x877c, 0x8894,
0x89bf, 0x8afc, 0x8c4b, 0x8dab, 0x8f1e, 0x90a1, 0x9236, 0x93dc,
0x9593, 0x975a, 0x9931, 0x9b18, 0x9d0e, 0x9f14, 0xa129, 0xa34c,
0xa57e, 0xa7be, 0xaa0b, 0xac65, 0xaecd, 0xb141, 0xb3c1, 0xb64c,
0xb8e4, 0xbb86, 0xbe32, 0xc0e9, 0xc3aa, 0xc674, 0xc946, 0xcc22,
0xcf05, 0xd1ef, 0xd4e1, 0xd7da, 0xdad8, 0xdddd, 0xe0e7, 0xe3f5,
0xe708, 0xea1e, 0xed38, 0xf055, 0xf375, 0xf696, 0xf9b9, 0xfcdc,
};

void main(void);
void OSCILLATOR_Init(void);
void DAC0_Init (void);
void DAC1_Init (void);
void TIMER4_Init(int counts);
void Set_DACs(void);

void main (void)


{
WDTCN = 0xde; // Disable watchdog timer
WDTCN = 0xad;
OSCILLATOR_Init (); // Initialize oscillator
DAC0_Init (); // Initialize DAC0
DAC1_Init (); // Initialize DAC1
TIMER4_Init(SYSCLK/SAMPLE_RATE_DAC);// Initialize Timer4 to overflow
EA = 1; // Enable global interrupts
while(1);
}

void TIMER4_ISR (void) interrupt 16


{
TF4 = 0; // Clear Timer4 overflow flag
Set_DACs();
}

void OSCILLATOR_Init (void)


{
int i; // Software timer
char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = CONFIG_PAGE; // set SFR page

Microprocessor and Microcontroller Bhumi Vavadiya


20/10/2023 Practical No.: 8 U20EE022

OSCICN = 0x83; // set internal oscillator to run at its max frequency


CLKSEL = 0x00; // Select the internal osc. as the SYSCLK source

//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){

char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page


SFRPAGE = DAC1_PAGE;
DAC1CN = 0x94; //Enable DAC1 in left-justified managed by T4 overflows
SFRPAGE = LEGACY_PAGE;
REF0CN |= 0x03; // Enable the internal VREF (2.4v) & Bias Generator
SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
}

void TIMER4_Init (int counts)


{
char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = TMR4_PAGE;

Microprocessor and Microcontroller Bhumi Vavadiya


20/10/2023 Practical No.: 8 U20EE022

TMR4CN = 0x00; // Stop Timer4; Clear overflow flag (TF4);Auto-Reload


TMR4CF = 0x08; // Configure Timer4 to increment;Timer4 counts SYSCLKs
RCAP4 = -counts; // Set reload value
TMR4 = RCAP4; // Initialzie Timer4 to reload value
EIE2 |= 0x04; // Enable Timer4 interrupts
TR4 = 1; // Start Timer4
SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
}

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:

Microprocessor and Microcontroller Bhumi Vavadiya


20/10/2023 Practical No.: 8 U20EE022

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.

Microprocessor and Microcontroller Bhumi Vavadiya


27/10/2023 Practical No.: 9 U20EE022

Software: SI LABS IDE Microcontroller: C8051f12x

[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;

#define SYSCLK 49000000


#define SAMPLE_RATE 5000
#define INT_DEC 256 // integrate and decimate
#define SAR_CLK 2500000

#define TIMER0_RELOAD_HIGH 0x3C


#define TIMER0_RELOAD_LOW 0xb0

sbit LED = P1^6;


sbit SW1 = P3^7;
sbit RS = P4^2;
sbit RW = P4^1;
sbit EN = P4^0;

unsigned int ia;


unsigned int measurement;

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];

Microprocessor and Microcontroller Bhumi Vavadiya


27/10/2023 Practical No.: 9 U20EE022

void main (void)


{
char SFRPAGE_SAVE = SFRPAGE;
WDTCN = 0xde;
WDTCN = 0xad;
OSCILLATOR_Init();
PORT_Init();
TIMER3_Init (SYSCLK/SAMPLE_RATE);
ADC0_Init();
Timer0_Init();
SFRPAGE = ADC0_PAGE;
AD0EN = 1;
lcd_initialize();
EA = 1;
while (1)
{
if(adc_flag==1)
{
measurement = result*2.4*1000/4095;
disp_data[0]=(measurement/10000) + 0x30;
disp_data[1]=((measurement%10000)/1000) + 0x30;
disp_data [2]=((measurement%1000)/100) + 0x30;
disp_data[3]=((measurement%100)/10) + 0x30;
disp_data[4]=(measurement%10) + 0x30;
}
if(timer0_flag)
{
adc_flag=0;
timer0_flag=0;
SFRPAGE = TMR3_PAGE;
TR3=0;
lcd_command(0x01);
send_lcd_data();
SFRPAGE = TMR3_PAGE;
TR3=1;
}
}
}

void OSCILLATOR_Init(void)
{
int loop;
char SFRPAGE_SAVE = SFRPAGE;
SFRPAGE = CONFIG_PAGE;
OSCICN = 0x83;

Microprocessor and Microcontroller Bhumi Vavadiya


27/10/2023 Practical No.: 9 U20EE022

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 PORT_Init (void)


{
char SFRPAGE_SAVE = SFRPAGE;
SFRPAGE = CONFIG_PAGE;
XBR0 = 0x37;
XBR1 = 0x7e;
XBR2 = 0x5c;
P7MDOUT=0xff;
P7=0x00;
P4MDOUT=0xff;
P4=0x00;
SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
}

void ADC0_Init (void)


{
char SFRPAGE_SAVE = SFRPAGE;
SFRPAGE = ADC0_PAGE;
ADC0CN = 0x04;
REF0CN = 0x07;
AMX0CF = 0x00; // AIN inputs are single ended
AMX0SL = 0x01; // select AIN0.1
ADC0CF = (SYSCLK/SAR_CLK/2) << 3; // ADC conversion clock = 2.5MHz
ADC0CF |= 0x00; // pga gain =1
EIE2|= 0x02; //enable adc interrupts
//ADC0CN |= 0x80;
SFRPAGE = SFRPAGE_SAVE;
}

Microprocessor and Microcontroller Bhumi Vavadiya


27/10/2023 Practical No.: 9 U20EE022

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;
}

void TIMER3_Init(int counts)


{
char SFRPAGE_SAVE = SFRPAGE;
SFRPAGE = TMR3_PAGE;
TMR3CN = 0x00;
TMR3CF = 0x08;
RCAP3 = -counts;
TMR3 = RCAP3;
EIE2 &= ~0x01; // disable t3 interrupts
TR3 = 1;
SFRPAGE = SFRPAGE_SAVE;
}

void ADC0_ISR (void) interrupt 15


{
static unsigned int_dec=INT_DEC,temp3;
static long accumulator=0L;
// ADC samples
char SFRPAGE_SAVE = SFRPAGE;
static int temp;
temp=ADC0H;
temp=temp<<8;
result=temp+ADC0L;
AD0INT = 0;
SFRPAGE = TMR3_PAGE;
TR3=0;
TMR3=RCAP3;
adc_flag=1;
SFRPAGE = SFRPAGE_SAVE;
}

Microprocessor and Microcontroller Bhumi Vavadiya


27/10/2023 Practical No.: 9 U20EE022

void Timer0_ISR (void) interrupt 1


{
char SFRPAGE_SAVE ;
static unsigned int int_counter=0;
SFRPAGE_SAVE = SFRPAGE;
TH0 = TIMER0_RELOAD_HIGH; // Reinit Timero High and Low register
TL0 = TIMER0_RELOAD_LOW;
int_counter++;
if(int_counter==20)
{
int_counter=0;
timer0_flag=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;
}

void lcd_initialize (void)


{
char SFRPAGE_SAVE = SFRPAGE;
SFRPAGE = CONFIG_PAGE;
lcd_command (0x30); // 5*7 Matrrix
lcd_command(0x0f); // display on cursor blinking
lcd_command (0x06); // entry mode
lcd_command (0x01); // clear display screen
SFRPAGE = SFRPAGE_SAVE;
return;
}

Microprocessor and Microcontroller Bhumi Vavadiya


27/10/2023 Practical No.: 9 U20EE022

void lcd_command (unsigned char temp1)


{
char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = CONFIG_PAGE;
lcd_busy();
RS=0;
RW=0;
P7=temp1;
EN=1;
for(ia=0;ia<5;ia++)
{
;
}
EN=0;
SFRPAGE = SFRPAGE_SAVE;
return;
}

void lcd_data(unsigned char temp2)


{
char SFRPAGE_SAVE = SFRPAGE;
SFRPAGE = CONFIG_PAGE;
lcd_busy ();
RS= 1;
RW=0;
P7=temp2;
EN=1;
for(ia=0;ia<50;ia++)
{
;
}
EN=0;
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++)
{
;
}
}

Microprocessor and Microcontroller Bhumi Vavadiya


27/10/2023 Practical No.: 9 U20EE022

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.

Microprocessor and Microcontroller Bhumi Vavadiya

You might also like