Pic 16 F
Pic 16 F
/*
Lab 1: Flashing LED with PIC16F688
Internal Oscillator @ 4MHz, MCLR Enabled, PWRT Enabled, WDT OFF
Copyright @ Rajendra Bhatt
Oct 7, 2010
*/
// Define LED @ RC0
sbit LED at RC0_bit;
void main() {
ANSEL = 0b00000000; //All I/O pins are configured as digital
CMCON0 = 007 ; // Disbale comparators
TRISC = 0b00000000; // PORTC All Outputs
TRISA = 0b00001000; // PORTA All Outputs, Except RA3
do {
LED = 1;
Delay_ms(1000);
LED = 0;
Delay_ms(1000);
} while(1); // Infinite Loop
}
// Initialize LCD
Do
{
Lcd_Cmd(_LCD_CLEAR);
// CLEAR display
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,4,message1);
// Cursor off
Lcd_Out(2,1,message2);
Delay_ms(1000);
Lcd_Cmd(_LCD_CLEAR);
// Clear display
// Infinite Loop
// Initialize LCD
Lcd_Cmd(_LCD_CLEAR);
// CLEAR display
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,1,message1);
// Cursor off
do {
adc_value = ADC_Read(2);
temp[0] = adc_value/1000 + 48; // Add 48 to get the ASCII character value
temp[1] = (adc_value/100)%10 + 48;
temp[2] = (adc_value/10)%10 + 48;
temp[3] = adc_value%10 + 48;
Lcd_Out(1,11,temp);
Delay_ms(2000);
} while(1);
}
// Initialize LCD
Lcd_Cmd(_LCD_CLEAR);
// CLEAR display
do {
TMR0=0;
Delay_ms(1000); // Delay 1 Sec
Display_Freq(TMR0);
} while(1); // Infinite loop
}
void debounce(){
Delay_ms(300);
}
void main() {
CMCON = 0x07; // Disable comparators
PORTB = 0x00;
TRISB = 0b00000011; // RB0, RB1 input, RB3 (PWM1) output
PWM1_Init(5000);
new_DC = 0;
current_DC = 0;
PWM1_Start();
PWM1_Set_Duty(current_DC);
do {
if (!UP){
debounce();
if (new_DC < 250)
// Don't go below 0
if (current_DC != new_DC) {
current_DC = new_DC ;
PWM1_Set_Duty(current_DC); // Change the current DC to new value
}
} while(1);
} // END main()
PinD = 1;
}
if (!Reverse)
{
GetDelay();
PORTC = 0;
GetDelay();
PinA = 1;
PinB = 0;
PinC = 1;
}
if (!Brake)
{
GetDelay();
PORTC = 0;
}
}
while(1);
} // END main()
void main() {
CMCON |= 7;
// Disable Comparators
do {
DD0 = Count%10; // Extract Ones Digit
DD0 = mask(DD0);
DD1 = (Count/10)%10; // Extract Tens Digit
DD1 = mask(DD1);
DD2 = (Count/100)%10; // Extract Hundreds Digit
DD2 = mask(DD2);
DD3 = (Count/1000); // Extract Thousands Digit
DD3 = mask(DD3);
RA1_bit = 1;
RA2_bit = 1;
RA3_bit = 1;
delay_ms(5);
PORTB = DD1;
RA0_bit = 1;
RA1_bit = 0;
RA2_bit = 1;
RA3_bit = 1;
delay_ms(5);
PORTB = DD2;
RA0_bit = 1;
RA1_bit = 1;
RA2_bit = 0;
RA3_bit = 1;
delay_ms(5);
PORTB = DD3;
RA0_bit = 1;
RA1_bit = 1;
RA2_bit = 1;
RA3_bit = 0;
delay_ms(5);
}
Count = Count + 1 ;
if (Count > 9999) Count = 0;
} while(1);
}
// endless loop
*/
};
// Display 26 alphabets
for (num=0; num<26; num++) {
} while(1);
PIC16F628A
Oscillator:
*/
// LCD connections definitions
sbit LCD_RS at RA0_bit;
sbit LCD_EN at RA1_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;
sbit LCD_RS_Direction at TRISA0_bit;
sbit LCD_EN_Direction at TRISA1_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD connections definitions
// Other connections
sbit SW1 at RB0_bit;
sbit SW2 at RB1_bit;
sbit SW3 at RB2_bit;
void Debounce(){
Delay_ms(100);
}
void main() {
PORTB = 0;
PORTA = 0;
TRISB = 0b00001111;
TRISA = 0b00100000;
CMCON=0x07;
Lcd_Init();
// Initialize LCD
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1, 1, "Lab 13: Testing");
Lcd_Out(2, 1, "internal EEPROM");
// Clear LCD
// Turn cursor off
Delay2s();
start:
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,message1);
Lcd_Out(1,8,message2);
Lcd_Out(2,1,message3);
do{
// Read Operation
if (!SW1) {
Debounce();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,message5);
ADD = 0;
for (i=0; i<16; i++) {
temp = EEPROM_Read(ADD+i);
Edata[i] = temp;
}
Lcd_Out(2,1,Edata);
Delay2s();
goto start;
}
// Write Operation
if (!SW2) {
Debounce();
ADD = 0;
for (i=0; i<16; i++) {
temp = message7[i];
EEPROM_Write(ADD+i,temp);
}
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,message4);
Delay2s();
goto start;
}
// Delete Operation
if (!SW3) {
Debounce();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,message6);
for (i=0; i<16; i++) {
temp = '0';
EEPROM_Write(ADD+i, temp);
}
Delay2s() ;
goto start;
}
} while(1);
void Delay2s(){
Delay_ms(2000);
}
I2C1_Wr(wAddrL);
I2C1_Wr(wData);
I2C1_Stop();
}
//--------------- Reads data from 24LC512 EEPROM - single location
unsigned short EEPROM_ReadByte(unsigned short EEPROM_Select, unsigned short rAddress)
{
unsigned short rAddrH, rAddrL, result;
rAddrH = 0;
rAddrL = rAddress;
I2C1_Start();
I2C1_Wr(rAddrL);
I2C1_Repeated_Start();
I2C1_Wr(EEPROM_Select+1);
result = I2C1_Rd(0u);
while (!I2C1_Is_Idle())
asm nop;
I2C1_Stop();
return result;
}
// Initialize LCD
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
// CLEAR display
// Cursor off
// Check EEPROM1
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,"EEPROM1");
check_device(EEPROM1);
Delay2s();
// Check EEPROM2
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,"EEPROM2");
check_device(EEPROM2);
Delay2s();
// Check Sensor
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,"DS1631");
check_device(DS1631);
Delay2s();
for (i=0; i<5; i++) {
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,"Reading Temp.");
// Read temperature
I2C1_Start();
I2C1_Wr(DS1631);
I2C1_Wr(0x51);
I2C1_Stop();
Delay_ms(750);
I2C1_Start();
I2C1_Wr(DS1631);
I2C1_Wr(0xAA);
I2C1_Repeated_Start();
I2C1_Wr(DS1631+1);
MS_Byte = I2C1_Rd(1);
LS_Byte = I2C1_Rd(0);
I2C1_Stop();
Delay2s();
// Write the MS Byte to EEPROM1
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,"Writing MS Byte");
EEPROM_WriteByte(EEPROM1, i, MS_Byte);
Delay2s();
// Write the LS Byte to EEPROM2
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,"Writing LS Byte");
EEPROM_WriteByte(EEPROM2, i, MS_Byte);
Delay2s();
}
// Reading stored temperature samples
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,"Reading EEPROMs");
Delay2s();
temp[5] = (Fraction_Temp/10)%10+48;
temp[6] = Fraction_Temp%10+48;
Lcd_Out(2,5,temp);
Delay2s();
}
/* CharData is a two dimensional constant array that holds the 8-bit column values of
individual rows for ASCII characters that are to be displayed on a 8x8 matrix format.
*/
const unsigned short CharData[][8] ={
{0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000},
{0b00000100, 0b00000100, 0b00000100, 0b00000100, 0b00000100, 0b00000100, 0b00000000,
0b00000100},
{0b00001010, 0b00001010, 0b00001010, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000},
{0b00000000, 0b00001010, 0b00011111, 0b00001010, 0b00011111, 0b00001010, 0b00011111,
0b00001010},
{0b00000111, 0b00001100, 0b00010100, 0b00001100, 0b00000110, 0b00000101, 0b00000110,
0b00011100},
{0b00011001, 0b00011010, 0b00000010, 0b00000100, 0b00000100, 0b00001000, 0b00001011,
0b00010011},
{0b00000110, 0b00001010, 0b00010010, 0b00010100, 0b00001001, 0b00010110, 0b00010110,
0b00001001},
{0b00000100, 0b00000100, 0b00000100, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000},
{0b00000010, 0b00000100, 0b00001000, 0b00001000, 0b00001000, 0b00001000, 0b00000100,
0b00000010},
{0b00001000, 0b00000100, 0b00000010, 0b00000010, 0b00000010, 0b00000010, 0b00000100,
0b00001000},
{0b00010101, 0b00001110, 0b00011111, 0b00001110, 0b00010101, 0b00000000, 0b00000000,
0b00000000},
{0b00000000, 0b00000000, 0b00000100, 0b00000100, 0b00011111, 0b00000100, 0b00000100,
0b00000000},
{0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000110, 0b00000100,
0b00001000},
speed = 10+ADC_Read(0)/10;
for(l=0; l<speed;l++){
send_data(DisplayBuffer[i]);
CD4017_Clk = 1;
CD4017_Clk = 0;
Delay_ms(1);
} // i
CD4017_Rst = 1;
CD4017_Rst = 0;
} // l
} // scroll
} // k
} while(1);
void main() {
ANSEL = 0b00000000; // All I/O pins are configured as digital
CMCON0 = 0x07 ;
// Disbale comparators
// Column connections
sbit Col1 at RB4_bit;
sbit Col2 at RB5_bit;
sbit Col3 at RB6_bit;
sbit Col4 at RB7_bit;
// Define Messages
char message1[] = "Hit Key:";
char KeyPad[] = " ";
unsigned short temp1, temp2;
unsigned short col, row;
char KeyS[4][4] ={
{0x31, 0x32, 0x33, 0x41},
char Get_Key(){
// Make rows output and columns input
TRISB = 0b11110000;
WPUB = 0b11110000;
PORTB = 0b11110000;
temp1 = (PORTB >> 4) ^ 0x0F;
col = temp1/2;
if(col == 4) col = 3;
// Make columns output and rows input
TRISB = 0b00001111;
WPUB = 0b00001111;
PORTB = 0b00001111;
temp2 = PORTB ^ 0x0F;
row = temp2/2;
if (row == 4) row = 3;
if (temp1 && temp2) return KeyS[row][col];
return ' ';
void main() {
char key[] = " ";
TRISA = 0b00100000; // PORTA output except RA5
TRISB = 0b00000000;
ANSELA = 0x00;
ANSELB = 0x00;
OPTION_REG = 0x00;
PORTB = 0b11111111; // All rows equal to 1
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
// CLEAR display
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,1,message1);
do {
key[0] = Get_Key();
if (key[0] != ' ') Lcd_Out(2,7,key);
}while(1);
}
// Column connections
sbit Col1 at RB4_bit;
sbit Col2 at RB5_bit;
sbit Col3 at RB6_bit;
sbit Col4 at RB7_bit;
// Cursor off
// Define Messages
char message1[] = "Hit Key:";
void Debounce(){
Delay_ms(50);
}
char Get_Key(){
// Scan row 1
PORTB = 0b00001110;
if (!Col1){
Debounce();
return 0x31;
}
if (!Col2){
Debounce();
return 0x32;
}
if (!Col3){
Debounce();
return 0x33;
}
if (!Col4){
Debounce();
return 0x41;
}
// Scan row 2
PORTB = 0b00001101;
if (!Col1){
Debounce();
return 0x34;
}
if (!Col2){
Debounce();
return 0x35;
}
if (!Col3){
Debounce();
return 0x36;
}
if (!Col4){
Debounce();
return 0x42;
}
// Scan row 3
PORTB = 0b00001011;
if (!Col1){
Debounce();
return 0x37;
}
if (!Col2){
Debounce();
return 0x38;
}
if (!Col3){
Debounce();
return 0x39;
}
if (!Col4){
Debounce();
return 0x43;
}
// Scan row 4
PORTB = 0b00000111;
if (!Col1){
Debounce();
return 0x2A;
}
if (!Col2){
Debounce();
return 0x30;
}
if (!Col3){
Debounce();
return 0x23;
}
if (!Col4){
Debounce();
return 0x44;
}
return ' ';
void main() {
char key[] = " ";
TRISA = 0b00100000; // PORTA output except RA5
TRISB = 0b11110000; // R1-R4 output, C1-C4 input
ANSELA = 0x00;
ANSELB = 0x00;
OPTION_REG = 0x00;
PORTB = 0b00001111; // All rows equal to 1
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
// CLEAR display
Lcd_Cmd(_LCD_CURSOR_OFF);
// Cursor off
Lcd_Out(1,1,message1);
WPUB = 0b11110000;
do {
key[0] = Get_Key();
if (key[0] != ' ') Lcd_Out(2,7,key);
}while(1);
}
unsigned int notes[] = {262, 262, 294, 262, 349, 330, 262, 262, 294, 262, 392,
/*
Hap py birth
F4 C4 C4 C5 A4 F4 E4 D4 B4b B4b A4 */
349, 262, 262, 523, 440, 349, 330, 294, 466, 466, 440,
/*
day to you
F4 G4 F4 */
349, 392, 349
};
unsigned short k;
void main() {
CMCON = 0x07;
TRISB = 0b00001000; // GP5, 5 I/P's, Rest O/P's
Sound_Init(&PORTB,0); // Initialize sound o/p pin
do {
for(k = 0; k<25; k++){
Sound_Play(notes[k], 100*interval[k]);
pause(6);
}
pause(100);
}while(1);
}
* Description:
This routine demonstrates how to initialize a KS0108 based GLCD and
activate the pixels on display. A sub-routine is written to draw a point on
the GLCD at a given coordinates.
* Test configuration:
MCU:
Dev.Board:
Oscillator:
PIC16F887
UNI-DS6
HS, 10.0000 MHz
*/
TRISD
void Enable_Pulse()
{
GLCD_EN = 1; //EN high
delay_us(5);
GLCD_EN = 0; //EN low
delay_us(5);
}
void GLCD_ON()
{
//Activate both chips
GLCD_CS1 = 0;
GLCD_CS2 = 0;
GLCD_RS = 0;
GLCD_RW = 0;
GLCD_Data = 0x3F;
Enable_Pulse();
}
GLCD_CS2 = 0;
GLCD_Data = 0xC0 | line;
Enable_Pulse();
}
GLCD_RW = 0;
if(x<64)
//left section
{
GLCD_CS1 = 0;
//select chip 1
GLCD_CS2 = 1;
//deselect chip 2
Col_Data = x;
}
else
//right section
GLCD_CS2 = 0;
GLCD_CS1 = 1;
Col_Data = x-64; //put column address on data port
}
Col_Data = (Col_Data | 0x40 ) & 0x7F; //Command format
GLCD_Data = Col_Data;
Enable_Pulse();
}
GLCD_RS = 0;
GLCD_RW = 0;
Col_Data = (y | 0xB8 ) & 0xBF; //put row address on data port set command
GLCD_Data = Col_Data;
Enable_Pulse();
}
GLCD_RW = 0;
GLCD_Data = b;
delay_us(1);
Enable_Pulse();
}
//PORTD as Input
//Read
GLCD_RS = 1;
//Data
GLCD_CS1 = (column>63);
GLCD_CS2 = !GLCD_CS1;
delay_us(1);
//tasu
//Disable/Enable CS2
GLCD_EN = 1;
delay_us(1);
//Dummy read
GLCD_EN = 0;
//Low Enable
delay_us(5);
GLCD_EN = 1;
delay_us(1);
//tr + td(twh)
read_data = GLCD_Data;
GLCD_EN = 0;
delay_us(1);
//Input data
GLCD_Dir = 0x00;
//Output again
return read_data;
}
GOTO_XY(64,ln);
GLCD_CS1 = 0;
for(i=0;i<65;i++)
GLCD_Write(0);
}
//-------------------------
void GLCD_CLR()
{
unsigned short m;
for(m=0;m<8;m++){
GLCD_Clrln(m);
}
}
//Light spot
//Dark spot
void main() {
unsigned short u, v;
ANSEL = 0;
ANSELH = 0;
C1ON_bit = 0;
C2ON_bit = 0;
TRISD = 0x00;
TRISB = 0x00;
PORTB = 0x00;
// Disable comparators
PORTD = 0x00;
GLCD_CS1 = 1;
GLCD_CS2 = 1;
GLCD_RST = 1;
GLCD_ON();
GLCD_CLR();
Set_Start_Line(0);
do {
} while(1);
}
void interrupt() {
delay = i*10;
Control = 1;
Delay_Cyc(delay); // Generates delay equal to 10*delay clock cycles
Control = 0;
TMR0 = 180;
void main() {
CMCON = 0x07; // Disable Comparators
TRISB = 0b00000001;
PORTB = 0;
OPTION_REG = 0x07; // Prescaler (1:256) is assigned to the timer TMR0
TMR0 = 180;
INTCON = 0xA0;
do{
if(!SW1){
Delay_ms(300);
i = i+2;
if(i>24) i=7;
}
}while(1);
}