ไมโครคอนโทลเลอร์ 3
code
#include "mcc_generated_files/mcc.h"
// test pattern 1111
// 1001
// 11111001 = 0xF9
// test pattern 1001
// 1111
// 10011111 = 0x 9F
uint8_t lvl_0_h = 0xFf;
uint8_t lvl_0_l = 0xff;
uint8_t lvl_1_h = 0xF9;
uint8_t lvl_1_l = 0x9f;
uint8_t lvl_2_h = 0xf9;
uint8_t lvl_2_l = 0x9f;
uint8_t lvl_3_h = 0xFf;
uint8_t lvl_3_l = 0xff;
uint8_t myledd;
uint8_t myledc;
int count;
void scan_cube(void){
// Select level 0 (top level)
LATB = 0x01;
// Scan columns
// We turn on two LEDs at a time, one from PORTD and one from PORTC
// Update RD7 and RC7
myledd = 0x00;
myledd = myledd | (lvl_0_h & 0x80);
LATD = ~myledd;
myledc = 0x00;
myledc = myledc | (lvl_0_l & 0x80);
LATC = ~myledc;
__delay_us(100);
// Update RD6 and RC6
myledd = 0x00;
myledd = myledd | (lvl_0_h & 0x40);
LATD = ~myledd;
myledc = 0x00;
myledc = myledc | (lvl_0_l & 0x40);
LATC = ~myledc;
__delay_us(100);
// Update RD5 and RC5
myledd = 0x00;
myledd = myledd | (lvl_0_h & 0x20);
LATD = ~myledd;
myledc = 0x00;
myledc = myledc | (lvl_0_l & 0x20);
LATC = ~myledc;
__delay_us(100);
// Update RD4 and RC4
myledd = 0x00;
myledd = myledd | (lvl_0_h & 0x10);
LATD = ~myledd;
myledc = 0x00;
myledc = myledc | (lvl_0_l & 0x10);
LATC = ~myledc;
__delay_us(100);
// Update RD3 and RC3
myledd = 0x00;
myledd = myledd | (lvl_0_h & 0x08);
LATD = ~myledd;
myledc = 0x00;
myledc = myledc | (lvl_0_l & 0x08);
LATC = ~myledc;
__delay_us(100);
// Update RD2 and RC2
myledd = 0x00;
myledd = myledd | (lvl_0_h & 0x04);
LATD = ~myledd;
myledc = 0x00;
myledc = myledc | (lvl_0_l & 0x04);
LATC = ~myledc;
__delay_us(100);
// Update RD1 and RC1
myledd = 0x00;
myledd = myledd | (lvl_0_h & 0x02);
LATD = ~myledd;
myledc = 0x00;
myledc = myledc | (lvl_0_l & 0x02);
LATC = ~myledc;
__delay_us(100);
// Update RD0 and RC0
myledd = 0x00;
myledd = myledd | (lvl_0_h & 0x01);
LATD = ~myledd;
myledc = 0x00;
myledc = myledc | (lvl_0_l & 0x01);
LATC = ~myledc;
__delay_us(100);
// Select level 1
LATB = 0x02;
// Scan columns
// We turn on two LEDs at a time, one from PORTD and one from PORTC
// Update RD7 and RC7
myledd = 0x00;
myledd = myledd | (lvl_1_h & 0x80);
LATD = ~myledd;
myledc = 0x00;
myledc = myledc | (lvl_1_l & 0x80);
LATC = ~myledc;
__delay_us(100);
// Update RD6 and RC6
myledd = 0x00;
myledd = myledd | (lvl_1_h & 0x40);
LATD = ~myledd;
myledc = 0x00;
myledc = myledc | (lvl_1_l & 0x40);
LATC = ~myledc;
__delay_us(100);
// Update RD5 and RC5
myledd = 0x00;
myledd = myledd | (lvl_1_h & 0x20);
LATD = ~myledd;
myledc = 0x00;
myledc = myledc | (lvl_1_l & 0x20);
LATC = ~myledc;
__delay_us(100);
// Update RD4 and RC4
myledd = 0x00;
myledd = myledd | (lvl_1_h & 0x10);
LATD = ~myledd;
myledc = 0x00;
myledc = myledc | (lvl_1_l & 0x10);
LATC = ~myledc;
__delay_us(100);
// Update RD3 and RC3
myledd = 0x00;
myledd = myledd | (lvl_1_h & 0x08);
LATD = ~myledd;
myledc = 0x00;
myledc = myledc | (lvl_1_l & 0x08);
LATC = ~myledc;
__delay_us(100);
// Update RD2 and RC2
myledd = 0x00;
myledd = myledd | (lvl_1_h & 0x04);
LATD = ~myledd;
myledc = 0x00;
myledc = myledc | (lvl_1_l & 0x04);
LATC = ~myledc;
__delay_us(100);
// Update RD1 and RC1
myledd = 0x00;
myledd = myledd | (lvl_1_h & 0x02);
LATD = ~myledd;
myledc = 0x00;
myledc = myledc | (lvl_1_l & 0x02);
LATC = ~myledc;
__delay_us(100);
// Update RD0 and RC0
myledd = 0x00;
myledd = myledd | (lvl_1_h & 0x01);
LATD = ~myledd;
myledc = 0x00;
myledc = myledc | (lvl_1_l & 0x01);
LATC = ~myledc;
__delay_us(100);
// Select level 2
LATB = 0x04;
// Scan columns
// We turn on two LEDs at a time, one from PORTD and one from PORTC
// Update RD7 and RC7
myledd = 0x00;
myledd = myledd | (lvl_2_h & 0x80);
LATD = ~myledd;
myledc = 0x00;
myledc = myledc | (lvl_2_l & 0x80);
LATC = ~myledc;
__delay_us(100);
// Update RD6 and RC6
myledd = 0x00;
myledd = myledd | (lvl_2_h & 0x40);
LATD = ~myledd;
myledc = 0x00;
myledc = myledc | (lvl_2_l & 0x40);
LATC = ~myledc;
__delay_us(100);
// Update RD5 and RC5
myledd = 0x00;
myledd = myledd | (lvl_2_h & 0x20);
LATD = ~myledd;
myledc = 0x00;
myledc = myledc | (lvl_2_l & 0x20);
LATC = ~myledc;
__delay_us(100);
// Update RD4 and RC4
myledd = 0x00;
myledd = myledd | (lvl_2_h & 0x10);
LATD = ~myledd;
myledc = 0x00;
myledc = myledc | (lvl_2_l & 0x10);
LATC = ~myledc;
__delay_us(100);
// Update RD3 and RC3
myledd = 0x00;
myledd = myledd | (lvl_2_h & 0x08);
LATD = ~myledd;
myledc = 0x00;
myledc = myledc | (lvl_2_l & 0x08);
LATC = ~myledc;
__delay_us(100);
// Update RD2 and RC2
myledd = 0x00;
myledd = myledd | (lvl_2_h & 0x04);
LATD = ~myledd;
myledc = 0x00;
myledc = myledc | (lvl_2_l & 0x04);
LATC = ~myledc;
__delay_us(100);
// Update RD1 and RC1
myledd = 0x00;
myledd = myledd | (lvl_2_h & 0x02);
LATD = ~myledd;
myledc = 0x00;
myledc = myledc | (lvl_2_l & 0x02);
LATC = ~myledc;
__delay_us(100);
// Update RD0 and RC0
myledd = 0x00;
myledd = myledd | (lvl_2_h & 0x01);
LATD = ~myledd;
myledc = 0x00;
myledc = myledc | (lvl_2_l & 0x01);
LATC = ~myledc;
__delay_us(100);
// Select level 3 (bottom level)
LATB = 0x08;
// Scan columns
// We turn on two LEDs at a time, one from PORTD and one from PORTC
// Update RD7 and RC7
myledd = 0x00;
myledd = myledd | (lvl_3_h & 0x80);
LATD = ~myledd;
myledc = 0x00;
myledc = myledc | (lvl_3_l & 0x80);
LATC = ~myledc;
__delay_us(100);
// Update RD6 and RC6
myledd = 0x00;
myledd = myledd | (lvl_3_h & 0x40);
LATD = ~myledd;
myledc = 0x00;
myledc = myledc | (lvl_3_l & 0x40);
LATC = ~myledc;
__delay_us(100);
// Update RD5 and RC5
myledd = 0x00;
myledd = myledd | (lvl_3_h & 0x20);
LATD = ~myledd;
myledc = 0x00;
myledc = myledc | (lvl_3_l & 0x20);
LATC = ~myledc;
__delay_us(100);
// Update RD4 and RC4
myledd = 0x00;
myledd = myledd | (lvl_3_h & 0x10);
LATD = ~myledd;
myledc = 0x00;
myledc = myledc | (lvl_3_l & 0x10);
LATC = ~myledc;
__delay_us(100);
// Update RD3 and RC3
myledd = 0x00;
myledd = myledd | (lvl_3_h & 0x08);
LATD = ~myledd;
myledc = 0x00;
myledc = myledc | (lvl_3_l & 0x08);
LATC = ~myledc;
__delay_us(100);
// Update RD2 and RC2
myledd = 0x00;
myledd = myledd | (lvl_3_h & 0x04);
LATD = ~myledd;
myledc = 0x00;
myledc = myledc | (lvl_3_l & 0x04);
LATC = ~myledc;
__delay_us(100);
// Update RD1 and RC1
myledd = 0x00;
myledd = myledd | (lvl_3_h & 0x02);
LATD = ~myledd;
myledc = 0x00;
myledc = myledc | (lvl_3_l & 0x02);
LATC = ~myledc;
__delay_us(100);
// Update RD0 and RC0
myledd = 0x00;
myledd = myledd | (lvl_3_h & 0x01);
LATD = ~myledd;
myledc = 0x00;
myledc = myledc | (lvl_3_l & 0x01);
LATC = ~myledc;
__delay_us(100);
}
void run_columns(void){
// scan outside columns
// 1000
// 0000 --> 0x80
// 0000
// 0000 --> 0x00
lvl_0_h = 0x80;
lvl_0_l = 0x00;
lvl_1_h = 0x80;
lvl_1_l = 0x00;
lvl_2_h = 0x80;
lvl_2_l = 0x00;
lvl_3_h = 0x80;
lvl_3_l = 0x00;
for (count = 0; count < 50; count++){
scan_cube();
}
// scan outside columns
// 0100
// 0000 --> 0x40
// 0000
// 0000 --> 0x00
lvl_0_h = 0x40;
lvl_0_l = 0x00;
lvl_1_h = 0x40;
lvl_1_l = 0x00;
lvl_2_h = 0x40;
lvl_2_l = 0x00;
lvl_3_h = 0x40;
lvl_3_l = 0x00;
for (count = 0; count < 50; count++){
scan_cube();
}
// scan outside columns
// 0010
// 0000 --> 0x20
// 0000
// 0000 --> 0x01
lvl_0_h = 0x20;
lvl_0_l = 0x00;
lvl_1_h = 0x20;
lvl_1_l = 0x00;
lvl_2_h = 0x20;
lvl_2_l = 0x00;
lvl_3_h = 0x20;
lvl_3_l = 0x00;
for (count = 0; count < 50; count++){
scan_cube();
}
// scan outside columns
// 0001
// 0000 --> 0x10
// 0000
// 0000 --> 0x00
lvl_0_h = 0x10;
lvl_0_l = 0x00;
lvl_1_h = 0x10;
lvl_1_l = 0x00;
lvl_2_h = 0x10;
lvl_2_l = 0x00;
lvl_3_h = 0x10;
lvl_3_l = 0x00;
for (count = 0; count < 50; count++){
scan_cube();
}
// scan outside columns
// 0000
// 0001 --> 0x01
// 0000
// 0000 --> 0x00
lvl_0_h = 0x01;
lvl_0_l = 0x00;
lvl_1_h = 0x01;
lvl_1_l = 0x00;
lvl_2_h = 0x01;
lvl_2_l = 0x00;
lvl_3_h = 0x01;
lvl_3_l = 0x00;
for (count = 0; count < 50; count++){
scan_cube();
}
// scan outside columns
// 0000
// 0000 --> 0x00
// 0001
// 0000 --> 0x10
lvl_0_h = 0x00;
lvl_0_l = 0x10;
lvl_1_h = 0x00;
lvl_1_l = 0x10;
lvl_2_h = 0x00;
lvl_2_l = 0x10;
lvl_3_h = 0x00;
lvl_3_l = 0x10;
for (count = 0; count < 50; count++){
scan_cube();
}
// scan outside columns
// 0000
// 0000 --> 0x00
// 0000
// 0001 --> 0x01
lvl_0_h = 0x00;
lvl_0_l = 0x01;
lvl_1_h = 0x00;
lvl_1_l = 0x01;
lvl_2_h = 0x00;
lvl_2_l = 0x01;
lvl_3_h = 0x00;
lvl_3_l = 0x01;
for (count = 0; count < 50; count++){
scan_cube();
}
// scan outside columns
// 0000
// 0000 --> 0x00
// 0000
// 0010 --> 0x02
lvl_0_h = 0x00;
lvl_0_l = 0x02;
lvl_1_h = 0x00;
lvl_1_l = 0x02;
lvl_2_h = 0x00;
lvl_2_l = 0x02;
lvl_3_h = 0x00;
lvl_3_l = 0x02;
for (count = 0; count < 50; count++){
scan_cube();
}
// scan outside columns
// 0000
// 0000 --> 0x00
// 0000
// 0100 --> 0x04
lvl_0_h = 0x00;
lvl_0_l = 0x04;
lvl_1_h = 0x00;
lvl_1_l = 0x04;
lvl_2_h = 0x00;
lvl_2_l = 0x04;
lvl_3_h = 0x00;
lvl_3_l = 0x04;
for (count = 0; count < 50; count++){
scan_cube();
}
// scan outside columns
// 0000
// 0001 --> 0x00
// 0000
// 1000 --> 0x08
lvl_0_h = 0x00;
lvl_0_l = 0x08;
lvl_1_h = 0x00;
lvl_1_l = 0x08;
lvl_2_h = 0x00;
lvl_2_l = 0x08;
lvl_3_h = 0x00;
lvl_3_l = 0x08;
for (count = 0; count < 50; count++){
scan_cube();
}
// scan outside columns
// 0000
// 0000 --> 0x00
// 1000
// 0000 --> 0x80
lvl_0_h = 0x00;
lvl_0_l = 0x80;
lvl_1_h = 0x00;
lvl_1_l = 0x80;
lvl_2_h = 0x00;
lvl_2_l = 0x80;
lvl_3_h = 0x00;
lvl_3_l = 0x80;
for (count = 0; count < 50; count++){
scan_cube();
}
// scan outside columns
// 0000
// 1000 --> 0x08
// 0000
// 0000 --> 0x00
lvl_0_h = 0x08;
lvl_0_l = 0x00;
lvl_1_h = 0x08;
lvl_1_l = 0x00;
lvl_2_h = 0x08;
lvl_2_l = 0x00;
lvl_3_h = 0x08;
lvl_3_l = 0x00;
for (count = 0; count < 50; count++){
scan_cube();
}
// scan inside columns
// 0000
// 0100 --> 0x04
// 0000
// 0000 --> 0x00
lvl_0_h = 0x04;
lvl_0_l = 0x00;
lvl_1_h = 0x04;
lvl_1_l = 0x00;
lvl_2_h = 0x04;
lvl_2_l = 0x00;
lvl_3_h = 0x04;
lvl_3_l = 0x00;
for (count = 0; count < 50; count++){
scan_cube();
}
// scan inside columns
// 0000
// 0010 --> 0x02
// 0000
// 0000 --> 0x00
lvl_0_h = 0x02;
lvl_0_l = 0x00;
lvl_1_h = 0x02;
lvl_1_l = 0x00;
lvl_2_h = 0x02;
lvl_2_l = 0x00;
lvl_3_h = 0x02;
lvl_3_l = 0x00;
for (count = 0; count < 50; count++){
scan_cube();
}
// scan inside columns
// 0000
// 0000 --> 0x00
// 0010
// 0000 --> 0x20
lvl_0_h = 0x00;
lvl_0_l = 0x20;
lvl_1_h = 0x00;
lvl_1_l = 0x20;
lvl_2_h = 0x00;
lvl_2_l = 0x20;
lvl_3_h = 0x00;
lvl_3_l = 0x20;
for (count = 0; count < 50; count++){
scan_cube();
}
// scan inside columns
// 0000
// 0000 --> 0x00
// 0100
// 0000 --> 0x40
lvl_0_h = 0x00;
lvl_0_l = 0x40;
lvl_1_h = 0x00;
lvl_1_l = 0x40;
lvl_2_h = 0x00;
lvl_2_l = 0x40;
lvl_3_h = 0x00;
lvl_3_l = 0x40;
for (count = 0; count < 50; count++){
scan_cube();
}
}
void play_rows(void){
// 1111
// 0000 --> 0xF0
// 0000
// 0000 --> 0x00
lvl_0_h = 0xF0;
lvl_0_l = 0x00;
lvl_1_h = 0x00;
lvl_1_l = 0x00;
lvl_2_h = 0x00;
lvl_2_l = 0x00;
lvl_3_h = 0x00;
lvl_3_l = 0x00;
for (count = 0; count < 50; count++){
scan_cube();
}
lvl_0_h = 0x00;
lvl_0_l = 0x00;
lvl_1_h = 0xF0;
lvl_1_l = 0x00;
lvl_2_h = 0x00;
lvl_2_l = 0x00;
lvl_3_h = 0x00;
lvl_3_l = 0x00;
for (count = 0; count < 50; count++){
scan_cube();
}
lvl_0_h = 0x00;
lvl_0_l = 0x00;
lvl_1_h = 0x00;
lvl_1_l = 0x00;
lvl_2_h = 0xF0;
lvl_2_l = 0x00;
lvl_3_h = 0x00;
lvl_3_l = 0x00;
for (count = 0; count < 50; count++){
scan_cube();
}
lvl_0_h = 0x00;
lvl_0_l = 0x00;
lvl_1_h = 0x00;
lvl_1_l = 0x00;
lvl_2_h = 0x00;
lvl_2_l = 0x00;
lvl_3_h = 0xF0;
lvl_3_l = 0x00;
for (count = 0; count < 50; count++){
scan_cube();
}
lvl_0_h = 0x00;
lvl_0_l = 0x00;
lvl_1_h = 0x00;
lvl_1_l = 0x00;
lvl_2_h = 0x00;
lvl_2_l = 0x00;
lvl_3_h = 0x0F;
lvl_3_l = 0x00;
for (count = 0; count < 50; count++){
scan_cube();
}
lvl_0_h = 0x00;
lvl_0_l = 0x00;
lvl_1_h = 0x00;
lvl_1_l = 0x00;
lvl_2_h = 0x0F;
lvl_2_l = 0x00;
lvl_3_h = 0x00;
lvl_3_l = 0x00;
for (count = 0; count < 50; count++){
scan_cube();
}
lvl_0_h = 0x00;
lvl_0_l = 0x00;
lvl_1_h = 0x0F;
lvl_1_l = 0x00;
lvl_2_h = 0x00;
lvl_2_l = 0x00;
lvl_3_h = 0x00;
lvl_3_l = 0x00;
for (count = 0; count < 50; count++){
scan_cube();
}
lvl_0_h = 0x0F;
lvl_0_l = 0x00;
lvl_1_h = 0x00;
lvl_1_l = 0x00;
lvl_2_h = 0x00;
lvl_2_l = 0x00;
lvl_3_h = 0x00;
lvl_3_l = 0x00;
for (count = 0; count < 50; count++){
scan_cube();
}
lvl_0_h = 0x00;
lvl_0_l = 0xF0;
lvl_1_h = 0x00;
lvl_1_l = 0x00;
lvl_2_h = 0x00;
lvl_2_l = 0x00;
lvl_3_h = 0x00;
lvl_3_l = 0x00;
for (count = 0; count < 50; count++){
scan_cube();
}
lvl_0_h = 0x00;
lvl_0_l = 0x00;
lvl_1_h = 0x00;
lvl_1_l = 0xF0;
lvl_2_h = 0x00;
lvl_2_l = 0x00;
lvl_3_h = 0x00;
lvl_3_l = 0x00;
for (count = 0; count < 50; count++){
scan_cube();
}
lvl_0_h = 0x00;
lvl_0_l = 0x00;
lvl_1_h = 0x00;
lvl_1_l = 0x00;
lvl_2_h = 0x00;
lvl_2_l = 0xF0;
lvl_3_h = 0x00;
lvl_3_l = 0x00;
for (count = 0; count < 50; count++){
scan_cube();
}
lvl_0_h = 0x00;
lvl_0_l = 0x00;
lvl_1_h = 0x00;
lvl_1_l = 0x00;
lvl_2_h = 0x00;
lvl_2_l = 0x00;
lvl_3_h = 0x00;
lvl_3_l = 0xF0;
for (count = 0; count < 50; count++){
scan_cube();
}
lvl_0_h = 0x00;
lvl_0_l = 0x00;
lvl_1_h = 0x00;
lvl_1_l = 0x00;
lvl_2_h = 0x00;
lvl_2_l = 0x00;
lvl_3_h = 0x00;
lvl_3_l = 0x0F;
for (count = 0; count < 50; count++){
scan_cube();
}
lvl_0_h = 0x00;
lvl_0_l = 0x00;
lvl_1_h = 0x00;
lvl_1_l = 0x00;
lvl_2_h = 0x00;
lvl_2_l = 0x0F;
lvl_3_h = 0x00;
lvl_3_l = 0x00;
for (count = 0; count < 50; count++){
scan_cube();
}
lvl_0_h = 0x00;
lvl_0_l = 0x00;
lvl_1_h = 0x00;
lvl_1_l = 0x0F;
lvl_2_h = 0x00;
lvl_2_l = 0x00;
lvl_3_h = 0x00;
lvl_3_l = 0x00;
for (count = 0; count < 50; count++){
scan_cube();
}
lvl_0_h = 0x00;
lvl_0_l = 0x0F;
lvl_1_h = 0x00;
lvl_1_l = 0x00;
lvl_2_h = 0x00;
lvl_2_l = 0x00;
lvl_3_h = 0x00;
lvl_3_l = 0x00;
for (count = 0; count < 50; count++){
scan_cube();
}
}
void cube_init (void){
// Clear all LEDs
LATD = 0xFF;
LATC = 0xFF;
LATB = 0x00;
}
/*
Main application
*/
void main(void)
{
// initialize the device
SYSTEM_Initialize();
// When using interrupts, you need to set the Global and Peripheral Interrupt Enable bits
// Use the following macros to:
// Enable the Global Interrupts
//INTERRUPT_GlobalInterruptEnable();
// Enable the Peripheral Interrupts
//INTERRUPT_PeripheralInterruptEnable();
// Disable the Global Interrupts
//INTERRUPT_GlobalInterruptDisable();
// Disable the Peripheral Interrupts
//INTERRUPT_PeripheralInterruptDisable();
// Layer connections:
// Layer 1 (top) = RB0
// Layer 2 = RB1
// Layer 3 = RB2
// Layer 4 (bottom) = RB3
// Column connections
// RD7 RD6 RD5 RD4
// RD3 RD2 RD1 RD0
// RC7 RC6 RC5 RC4
// RC3 RC2 RC1 RC0
while (1)
{
// Light all LEDs
lvl_0_h = 0xFF;
lvl_0_l = 0xFF;
lvl_1_h = 0xFF;
lvl_1_l = 0xFF;
lvl_2_h = 0xFF;
lvl_2_l = 0xFF;
lvl_3_h = 0xFF;
lvl_3_l = 0xFF;
// run for about 1.25 seconds
for (count = 0; count < 600; count++){
scan_cube();
}
// turn everything off
cube_init();
__delay_ms(1000);
// Turn each column on
run_columns();
cube_init();
__delay_ms(1000);
play_rows();
cube_init();
__delay_ms(1000);
}
}
ไม่มีความคิดเห็น:
แสดงความคิดเห็น