/
***********************************************************************************
*************************************/
// Experiment 1:
/
***********************************************************************************
*************************************/
#include <xc.h>
#define _XTAL_FREQ 8000000 // Define crystal frequency as 8 MHz
void main(void) {
TRISBbits.TRISB0 = 0; // Set RB0 as output for LED
TRISCbits.TRISC0 = 1; // Set RC0 as input for Switch
while (1) {
if (PORTCbits.RC0 == 1) { // If switch is pressed
PORTBbits.RB0 = 1; // Turn on LED
} else {
// If switch is not pressed
PORTBbits.RB0 = 0; // Turn off LED
}
}
}
/
***********************************************************************************
*************************************/
// Experiment 2:
/
***********************************************************************************
*************************************/
#include <xc.h>
#define _XTAL_FREQ 8000000
void main() {
char seg_code_ca[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80,
0x90}; //For common anode
char seg_code_cc[] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f,
0x6f}; // For common cathode
int i;
/* Configure the ports as output */
TRISB = 0x00;
//Configure PORTB as output
TRISC = 0x00;
//Configure PORTC as output
while (1) {
// loop to display 0-9 on CC
for (i = 0; i <= 9; i++) {
PORTC = 0x03;
PORTB = seg_code_cc[i];
__delay_ms(500);
}
// loop to display 0-9 on CA
for (i = 0; i <= 9; i++) {
PORTC = 0x00;
PORTB = seg_code_ca[i];
__delay_ms(500);
}
}
}
/
***********************************************************************************
*************************************/
// Experiment 3:
/
***********************************************************************************
*************************************/
#include <xc.h>
//define crystal frequency to 8 MHz
#define _XTAL_FREQ 8000000
#define digit1 PORTBbits.RB0
#define digit2 PORTBbits.RB1
#define digit3 PORTBbits.RB2
#define digit4 PORTBbits.RB3
// This array stores binary bit pattern that will be send to PORTD
unsigned char binary_pattern[] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07,
0x7F, 0x6F};
unsigned int a, b, c, d;
unsigned int adcval;
unsigned int counter = 0;
void InitADC(void)
{
TRISAbits.TRISA0 = 1;
ADCON0bits.ADCS1 = 0;
ADCON0bits.ADCS0 = 1;
ADCON0bits.CHS = 0b000;
ADCON0bits.GO = 0;
ADCON0bits.ADON = 1;
ADCON1bits.ADFM = 1;
ADCON1bits.ADCS2 = 1;
ADCON1bits.PCFG = 0b1110;
}
unsigned int getADCvalue()
{
__delay_ms(10);
ADCON0bits.GO = 1;
while (ADCON0bits.nDONE == 1);
return (unsigned int)((ADRESH << 8) + ADRESL);
}
void main(void)
{
TRISD = 0x00;
PORTD = 0X00;
TRISB = 0X00;
TRISC = 0x00;
InitADC();
digit1 = 1;
digit2 = 1;
digit3 = 1;
digit4 = 1;
while (1) {
//define PORTD as a output pin
// initialize PORTD pins to active low
adcval = getADCvalue(); //this specifies the channel value
a = adcval / 1000; // holds 1000's digit
b = ((adcval / 100) % 10); // holds 100's digit
c = ((adcval / 10) % 10); // holds 10th digit
d = (adcval % 10); // holds unit digit value
PORTD = binary_pattern[a]; // send 1000's place data to fourth digit
digit1 = 0; // turn on forth display unit
__delay_ms(5);
digit1 = 1; // turn off forth display unit
PORTD = binary_pattern[b]; // send 100's place data to 3rd digit
digit2 = 0;
// turn on 3rd display unit
__delay_ms(5);
digit2 = 1; // turn off 3rd display unit
PORTD = binary_pattern[c]; // send 10th place data to 2nd digit
digit3 = 0; // turn on 2nd display unit
__delay_ms(5);
digit3 = 1; // turn off 2nd display unit
PORTD = binary_pattern[d]; // send unit place data to 1st digit
digit4 = 0; // turn on 1st display unit
__delay_ms(5);
digit4 = 1; // turn off 1st display unit
if (adcval > 511) {
RC0 = 1;
__delay_ms(5);
} else {
RC0 = 0;
__delay_ms(5);
}
}
return;
}
/
***********************************************************************************
*************************************/
// Experiment 4:
/
***********************************************************************************
*************************************/
#include <xc.h>
#define _XTAL_FREQ 8000000
#define LcdDataPort PORTB
#define LcdControlPort PORTD
#define RS 0
#define RW 1
#define EN 2
/* Function to send the command to LCD */
void Lcd_CmdWrite(char lcd_cmd) {
LcdDataPort = lcd_cmd;
//Send the Command nibble
LcdControlPort &= ~(1 << RS); // Send LOW pulse on RS pin for selecting Command
register
LcdControlPort &= ~(1 << RW); // Send LOW pulse on RW pin for Write operation
LcdControlPort |= (1 << EN); // Generate a High-to-low pulse on EN pin
__delay_ms(100);
LcdControlPort &= ~(1 << EN);
__delay_ms(100);
}
/* Function to send the Data to LCD */
void Lcd_DataWrite(char lcd_data) {
LcdDataPort = lcd_data;
LcdControlPort |= (1 << RS);
//register
LcdControlPort &= ~(1 << RW);
//operation
LcdControlPort |= (1 << EN);
__delay_ms(100);
LcdControlPort &= ~(1 << EN);
__delay_ms(100);
}
void main() {
TRISB = 0x00; // Configure all the LCD pins as output
TRISD = 0x00; // Configure the Ctrl pins as output
Lcd_CmdWrite(0x38);
Lcd_CmdWrite(0x0E);
Lcd_CmdWrite(0x01);
Lcd_CmdWrite(0x80);
Lcd_DataWrite('W');
Lcd_DataWrite('e');
Lcd_DataWrite('l');
Lcd_DataWrite('c');
Lcd_DataWrite('o');
Lcd_DataWrite('m');
Lcd_DataWrite('e');
while (1);
}