Professional Documents
Culture Documents
MMS Snipings
MMS Snipings
MMS Snipings
#include <xc.h>
#include <stdio.h>
// Configuration bits
#pragma config FOSC = HS // High-speed oscillator (HS)
#pragma config WDTE = OFF // Watchdog Timer disabled
#pragma config PWRTE = OFF // Power-up Timer disabled
#pragma config BOREN = ON // Brown-out Reset enabled
#pragma config LVP = OFF // Low-Voltage Programming disabled
#pragma config CPD = OFF // Data memory Code Protection disabled
#pragma config WRT = OFF // Flash Memory Write Protection off
#pragma config CP = OFF // Flash Memory Code Protection off
void main() {
int primeCount = 0;
int sum = 0;
if (primeCount == 2) {
PORTB = sum; // Display the sum on PORTB
} else {
PORTB = 0xFF; // Indicate error on PORTB (all LEDs ON)
}
while (1) { call DELAY
// Infinite loop to keep the program running
MOVLW b'11100000'
} MOVWF PORTC
} call DELAY
MOVLW b'11110000'
MOVWF PORTC
call DELAY
MOVLW b'11111000'
LAB4TASK1A MOVWF PORTC
include<p16f877a.inc> call DELAY
__CONFIG 3F3A MOVLW b'11111100'
movlw 0xFF MOVWF PORTC
movwf 20h call DELAY
movwf 21h MOVLW b'11111110'
COUNT1 equ 20h MOVWF PORTC
COUNT2 equ 21h call DELAY
MOVLW b'11111111'
BSF STATUS,RP0 MOVWF PORTC
MOVLW B'00000000' call DELAY
MOVWF TRISC
MOVLW B'11111111'
MOVWF TRISB GOTO CLEAR
CLEAR:
BCF STATUS,RP0 MOVLW 00h
CLRF PORTC MOVWF PORTC
LOOP:
BTFSC PORTB,4 DELAY
GOTO ON decfsz COUNT1,1
OFF: goto DELAY
MOVLW 00h decfsz COUNT2,1
MOVWF PORTC goto DELAY
GOTO LOOP return
ON: end
MOVLW b'00000001'
MOVWF PORTC
call DELAY
MOVLW b'00000011'
MOVWF PORTC
call DELAY
MOVLW b'00000111'
MOVWF PORTC
call DELAY
MOVLW b'00001111'
MOVWF PORTC
call DELAY
MOVLW b'00011111'
MOVWF PORTC
call DELAY
MOVLW b'00111111'
MOVWF PORTC
call DELAY
MOVLW b'01111111'
MOVWF PORTC
call DELAY
MOVLW b'11111111'
MOVWF PORTC
call DELAY
MOVLW b'10000000'
MOVWF PORTC
call DELAY
MOVLW b'11000000'
MOVWF PORTC
LAB5TASK3
include<p16f877A.inc>
__CONFIG 3F3A
; Define constants
COUNT1 equ 20h
COUNT2 equ 21h
BUTTON_PORT equ PORTA ; Push buttons connected to Port A
OUTPUT_PORT equ PORTD ; Output port to display the count
bsf STATUS,5
movlw 0x00
movwf TRISD
bcf STATUS,5
MAIN
movlw 0x00 ; Initialize count to 0
movwf OUTPUT_PORT ; Set initial count on OUTPUT_PORT
movlw 0x00 ; Clear W register
movwf COUNT1 ; Clear COUNT1
movwf COUNT2 ; Clear COUNT2
LOOP_BUTTON:
btfss BUTTON_PORT, 0 ; Check if button 1 is pressed (assuming it's connected to bit 0 of BUTTON_PORT,
i.e., RA0)
goto CHECK_BUTTON_2 ; Check button 2 if button 1 is not pressed
CHECK_BUTTON_2:
btfss BUTTON_PORT, 1 ; Check if button 2 is pressed (assuming it's connected to bit 1 of BUTTON_PORT,
i.e., RA1)
goto LOOP_BUTTON ; If neither button is pressed, continue loop
DELAY
Loop decfsz COUNT1,1
goto Loop
decfsz COUNT2,1
goto Loop
return
end
LAB4TASK4
#include <xc.h>
#include <stdio.h>
// Configuration bits
#pragma config FOSC = HS // High-speed oscillator (HS)
#pragma config WDTE = OFF // Watchdog Timer disabled
#pragma config PWRTE = OFF // Power-up Timer disabled
#pragma config BOREN = ON // Brown-out Reset enabled
#pragma config LVP = OFF // Low-Voltage Programming disabled
#pragma config CPD = OFF // Data memory Code Protection disabled
#pragma config WRT = OFF // Flash Memory Write Protection off
#pragma config CP = OFF // Flash Memory Code Protection off
void main() {
int primeCount = 0;
int sum = 0;
if (primeCount == 2) {
PORTB = sum; // Display the sum on PORTB
} else {
PORTB = 0xFF; // Indicate error on PORTB (all LEDs ON)
}
while (1) {
// Infinite loop to keep the program running
}
}
LAB4TASK1A
#include <p16f877a.inc>
__CONFIG 3F3A
org 0x00
goto START
START:
BSF STATUS, RP0 ; Select Bank 1
MOVLW B'00000000' ; Set PORTB as output
MOVWF TRISB
MOVLW B'11111111' ; Set PORTA as input
MOVWF TRISA
MOVLW B'00000000' ; Set PORTC as output
MOVWF TRISC
BCF STATUS, RP0 ; Select Bank 0
CLRF PORTB ; Clear PORTB
CLRF PORTC ; Clear PORTC
LOOP:
BTFSC PORTB, 4 ; Check if button is pressed (RB4)
GOTO SEQ_ON
GOTO SEQ_OFF ; If button not pressed, turn off LEDs
SEQ_ON:
MOVLW 0x01 ; Start with RC0
MOVWF PORTC
CALL DELAY
INCF PORTC, f
CALL DELAY
INCF PORTC, f
CALL DELAY
; Subroutine to convert a digit (0-9) to 7-segment
INCF PORTC, f
display code
CALL DELAY
DIGIT_TO_SEGMENTS:
INCF PORTC, f
ADDWF PCL, F
CALL DELAY
RETLW B'00111111' ;0
INCF PORTC, f
RETLW B'00000110' ;1
CALL DELAY
RETLW B'01011011' ;2
INCF PORTC, f
RETLW B'01001111' ;3
CALL DELAY
RETLW B'01100110' ;4
INCF PORTC, f
RETLW B'01101101' ;5
CALL DELAY
RETLW B'01111101' ;6
GOTO SEQ_OFF_LOOP
RETLW B'00000111' ;7
RETLW B'01111111' ;8
SEQ_OFF_LOOP:
RETLW B'01101111' ;9
MOVLW 0x80 ; Start with RC7
MOVWF PORTC
; Clear PORTB
CALL DELAY
CLEAR:
DECF PORTC, f
CLRF PORTB
CALL DELAY
return
DECF PORTC, f
CALL DELAY
; Delay function
DECF PORTC, f
DELAY:
CALL DELAY
movlw 0xFF
LAB4TASK2A
movwf COUNT1
include <p16f877a.inc>
movwf COUNT2
__CONFIG 3F3A
DELAY_LOOP:
decfsz COUNT1, 1
; Define constants
goto DELAY_LOOP
COUNT1 equ 20h
DECF PORTC,
decfszf COUNT2, 1
COUNT2 equ 21h
CALL DELAY
goto DELAY_LOOP
DECF PORTC,
return f
; Initialize ports
CALL DELAY
org 0x00
DECF PORTC,
end f
goto START
CALL DELAY
DECF PORTC, f
org 0x05
CALL DELAY
START:
GOTO LOOP
BSF STATUS, RP0 ; Switch to Bank 1
MOVLW B'00000000'
SEQ_OFF:
MOVWF TRISB ; Set PORTB as output
MOVLW 00h ; Turn off all LEDs on PORTC
MOVLW B'11111111'
MOVWF PORTC
MOVWF TRISA ; Set PORTA as input
GOTO LOOP
BCF STATUS, RP0 ; Switch back to Bank 0
DELAY:
; Clear ports
MOVLW 0xFF ; Load COUNT1 with 0xFF
CLRF PORTB
MOVWF COUNT1
MOVLW 0xFF ; Load COUNT2 with 0xFF
; Main program loop
MOVWF COUNT2
LOOP:
DELAY_LOOP1:
BTFSC PORTA, 4 ; Test bit 4 of PORTA
decfsz COUNT1, f ; Decrement COUNT1
GOTO DISPLAY_LOOP
goto DELAY_LOOP1
GOTO OFF ; Otherwise, go to OFF
decfsz COUNT2, f ; Decrement COUNT2
goto DELAY_LOOP1
return
end
OFF:
CLRF PORTB ; Clear PORTB decfsz COUNT2, 1
GOTO LOOP goto DELAY_LOOP
return
DISPLAY_LOOP:
; Loop to display digits 0 to 9 end
MOVLW 0x00 ; Start with 0
MOVWF COUNT1
DISPLAY_DIGITS:
; Display the current digit on PORTB
CALL DISPLAY_DIGIT
CALL DELAY ; Delay
INCF COUNT1, F ; Increment to next digit
MOVF COUNT1, W
XORLW 0x0A ; Check if 10 (end of loop)
BTFSS STATUS, Z ; If not zero, loop back
GOTO DISPLAY_DIGITS
; Define constants
COUNT1 equ 20h
COUNT2 equ 21h
; Initialize ports
org 0x00
goto START
org 0x05
START:
BSF STATUS, RP0 ; Switch to Bank 1
MOVLW B'00000000'
MOVWF TRISB ; Set PORTB as output
MOVLW B'11111111'
MOVWF TRISA ; Set PORTA as input
BCF STATUS, RP0 ; Switch back to Bank 0
; Clear ports
CLRF PORTB
OFF:
CLRF PORTB ; Clear PORTB
GOTO LOOP
DISPLAY_001:
; Display 001 on PORTB
MOVLW B'00000001' ; Binary value 001
MOVWF PORTB
call DELAY ; Delay
call CLEAR ; Clear PORTB
call DELAY ; Delay
; Clear PORTB
CLEAR:
CLRF PORTB
return
; Delay function
DELAY:
movlw 0xFF
movwf COUNT1
movwf COUNT2
DELAY_LOOP:
decfsz COUNT1, 1
goto DELAY_LOOP
LAB5TASK2
include<p16f877A.inc>
__CONFIG 3F3A
; Define constants
COUNT1 equ 20h
COUNT2 equ 21h
BUTTON_PORT equ PORTA ; Push button connected to Port A
OUTPUT_PORT equ PORTD ; Output port to display the count
bsf STATUS,5
movlw 0x00
movwf TRISD
bcf STATUS,5
MAIN
movlw 0x00 ; Initialize count to 0
movwf OUTPUT_PORT ; Set initial count on OUTPUT_PORT
movlw 0x00 ; Clear W register
movwf COUNT1 ; Clear COUNT1
movwf COUNT2 ; Clear COUNT2
LOOP_BUTTON:
btfss BUTTON_PORT, 0 ; Check if button is pressed (assuming it's connected to bit 0 of BUTTON_PORT,
i.e., RA0)
goto LOOP_BUTTON ; Wait until the button is pressed
DELAY
Loop decfsz COUNT1,1
goto Loop
decfsz COUNT2,1
goto Loop
return
end
LAB5TASK6
include<p16f877A.inc>
__CONFIG 3F3A
movlw 0xFF
movwf 20h
movwf 21h
COUNT1 equ 20h
COUNT2 equ 21h
bsf STATUS,5
movlw 0x00
movwf TRISD
bcf STATUS,5
MAIN
Movlw 79h
movwf PORTD
call DELAY
call DELAY
call DELAY
movlw 38h LAB6TASK2
LAB6TASK1
movwf PORTD sbit Motor_Clockwise at RC0_bit; // Button for clockwise
int i=0;
call DELAY direction
void main() {
call DELAY sbit TRISC.F0=1;
Motor_Anticlockwise at RC1_bit; // Button for
call DELAY anticlockwise direction
TRISC.F1=1;
movlw 40h sbit TRISC.F2=1;
Motor_Output at RD0_bit; // Output pin for motor
movwf PORTD
call DELAY TRISC.F3=1;
voidTRISD
main()= {0x00;
call DELAY TRISC0_bit = 1; // Set RC0 as input for clockwise button
call DELAY TRISC1_bit
while(1) {= 1; // Set RC1 as input for anticlockwise button
movlw 4Fh TRISD0_bit = 0; //
if(PORTC.F0 ==Set
0) {RD0 as output for motor
movwf PORTD
call DELAY PORTD= 6 + 2;
Motor_Output
} = 0; // Initially turn off motor
call DELAY
call DELAY else if(PORTC.F1 == 0) {
while(1) {
PORTD = 6 * 2;
movlw 5Bh if(Motor_Clockwise)
} { // If clockwise button is pressed
movwf PORTD
call DELAY else if(PORTC.F2 == 0)= {1; // Turn on motor in clockwise
Motor_Output
directionPORTD= 6 / 2;
call DELAY } }
call DELAY else
elseif(Motor_Anticlockwise)
if(PORTC.F3 == 0) { { // If anticlockwise button is
movlw 7Fh pressed PORTD= 6 - 2;
movwf PORTD
} Motor_Output = 0; // Turn off motor (or change
call DELAY direction if required)
call DELAY } delay_ms(500);
call DELAY } }
goto MAIN } }
DELAY
Loop decfsz COUNT1,1
goto Loop
decfsz COUNT2,1
goto Loop
return
end
LAB6TASK3
int i=0;
void main()
{
TRISC=0X00;
TRISB.F4=1;
PORTC=0X00;
while(1)
{
if(PORTB.F4=1)
{
PORTC=1;
delay_ms(500);
PORTC=2;
delay_ms(500);
PORTC=3;
delay_ms(500);
PORTC=4;
delay_ms(500);
PORTC=5;
delay_ms(500);
PORTC=6;
delay_ms(500);
PORTC=7;
delay_ms(500);
PORTC=6;
delay_ms(500);
PORTC=5;
delay_ms(500);
PORTC=4;
delay_ms(500);
PORTC=3;
delay_ms(500);
PORTC=2;
delay_ms(500);
PORTC=1;
delay_ms(500);
}
else
{
PORTC=0;
}
}
}
LAB7TASK1
#include <stdio.h>
void Move_Delay() {
Delay_ms(500);
}
void main() {
char i; // Declare i outside the loop
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Delay_ms(2000);
Lcd_Cmd(_LCD_CLEAR);
writeToLCD(name, ID);
Delay_ms(2000);
while(1) {
for(i = 0; i < 8; i++) { // Use i directly in the loop
Lcd_Cmd(_LCD_SHIFT_LEFT);
Move_Delay();
}
for(i = 0; i < 8; i++) { // Use i directly in the loop
Lcd_Cmd(_LCD_SHIFT_RIGHT);
Move_Delay();
}
}
}
LAB7TASK3
#include <stdio.h>
void main() {
char character = 'A'; // Initialize character variable
// Display characters from 'A' to 'Z' on the 1st row, 7th column
Lcd_Chr(1, 7, ' '); // Clear the 7th column
for (i = 0; i < 26; i++) {
Lcd_Chr(1, 7, character++);
Move_Delay();
}
#include <stdio.h>
// LCD module connections
sbit LCD_RS at RB4_bit; }
sbit LCD_EN at RB5_bit; }
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit; LAB7TASK4
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections
void main() {
int counter;
counter = 0;
while(1) { // Endless loop
// Count from 0 to 99
for(counter = 0; counter < 100; counter++) {
displayCounter(counter);
Delay_ms(500); // Delay for 0.5 second
}
// Count from 99 to 0
for(counter = 99; counter >= 0; counter--) {
displayCounter(counter);
Delay_ms(500); // Delay for 0.5 second
LAB8TASK1
}
unsigned short kp, oldstate = 0;
char txt[6];
void main() {
unsigned char cnt = 0; // Initialize counter
while (1) {
do {
kp = Keypad_Key_Click(); // Store key code in kp variable
} while (!kp);
LAB8 TASK4
void main() {
num1 = 0;
num2 = 0;
result = 0;
operation = 0;
while (1) {
kp = 0;
do
kp = Keypad_Key_Click(); // Wait for key to be pressed and released
while (!kp);
switch (kp) {
case 1: kp = '1'; break;
case 2: kp = '2'; break;
case 3: kp = '3'; break;
case 4: kp = '+'; break;
case 5: kp = '4'; break;
case 6: kp = '5'; break;
case 7: kp = '6'; break;
case 8: kp = '-'; break;
case 9: kp = '7'; break;
case 10: kp = '8'; break;
case 11: kp = '9'; break;
case 12: kp = '*'; break;
case 13: kp = 'C'; break;
case 14: kp = '0'; break;
case 15: kp = '='; break;
case 16: kp = '/'; break;
}
if (kp == 'C') {
// Clear everything
num1 = 0;
num2 = 0;
result = 0;
operation = 0;
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1, 1, "Simple Calc");
} else if (kp == '=') {
// Perform the operation
if (operation == '+') {
result = num1 + num2;
} else if (operation == '-') {
result = num1 - num2;
} else if (operation == '*') {
result = num1 * num2;
} else if (operation == '/') {
if (num2 != 0) {
result = num1 / num2;
} else {
result = 0; // Handle division by zero
}
}
// Display result
WordToStr(result, txt);
Lcd_Out(2, 1, txt);
} else if (kp == '+' || kp == '-' || kp == '*' || kp == '/') {
operation = kp;
Lcd_Chr(1, 8, kp); // Display operation
} else {
// If no operation yet, store in num1
if (operation == 0) {
num1 = num1 * 10 + (kp - '0');
Lcd_Chr(1, 8, kp); // Display number
} else {
// Otherwise, store in num2
num2 = num2 * 10 + (kp - '0');
Lcd_Chr(2, 8, kp); // Display number
}
}
}
}