danielyen93
Newbie level 2
- Joined
- Feb 3, 2013
- Messages
- 2
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,281
- Activity points
- 1,301
/* 7 Segment Dice */
unsigned char key ( void )
{
unsigned char i;
unsigned char oldv, newv, mask ;
// debounce PORTA
oldv = PORTA ;
i = 0 ;
while ( i < 20 )
{
newv = PORTA ;
if ( oldv == newv )
{
i++ ;
}
else
{
i = 0 ;
oldv = newv ;
}
}
// Find which button
if ( newv != 0 )
{
mask = 1 ;
// Only look for first 3 inputs
for ( i=0 ; i<3 ; i=i+1 )
{
if ( PORTA & mask )
{
return i + 1 ;
}
// shift on to the next bit
mask = mask << 1 ;
}
}
// No button
return 0 ;
}
void main (void)
{
unsigned int i ;
unsigned int j ;
unsigned char r;
unsigned char dig [6];
dig[0] = 249;
dig[1] = 146;
dig[2] = 176;
dig[3] = 153;
dig[4] = 146;
dig[5] = 130;
/* Select the Register bank 1 */
set_bit ( STATUS, RP0 ) ;
/* set all of PORTB for output */
TRISB = 0x00 ;
/* set all of PORTA for input */
TRISA = 0x1f ;
/* now use Register bank 0 */
clear_bit ( STATUS, RP0 ) ;
// set all led's on
PORTB = 0;
// delay for 2 seconds
delay_s(2);
// set all LEDs off
PORTB = 255;
// Main program loop
r = 0;
while (1) {
// Wait for Switch A0
while ( key () != 1 );
while ( key () == 1 ) {
// generate a random number
r = r + 1;
if ( r == 6 )
r = 0;
}
// set all LEDs off
PORTB = 255;
// Rotate display 3 times
for ( i=0 ; i < 3 ; i= i + 1 ) {
for ( j=0 ; j < 6 ; j= j + 1 ) {
output_low_port_b ( j ) ;
if ( j == 0 ) {
output_high_port_b ( 5 ) ;
} else {
output_high_port_b ( j - 1 ) ;
}
delay_ms(100);
}
}
// Turn off led 5
output_high_port_b ( 5 ) ;
// Display number
PORTB = dig[r];
delay_s(1);
}
}
- - - Updated - - -
error at set_bit ( STATUS, RP0 ) ;
unsigned char key ( void )
{
unsigned char i;
unsigned char oldv, newv, mask ;
// debounce PORTA
oldv = PORTA ;
i = 0 ;
while ( i < 20 )
{
newv = PORTA ;
if ( oldv == newv )
{
i++ ;
}
else
{
i = 0 ;
oldv = newv ;
}
}
// Find which button
if ( newv != 0 )
{
mask = 1 ;
// Only look for first 3 inputs
for ( i=0 ; i<3 ; i=i+1 )
{
if ( PORTA & mask )
{
return i + 1 ;
}
// shift on to the next bit
mask = mask << 1 ;
}
}
// No button
return 0 ;
}
void main (void)
{
unsigned int i ;
unsigned int j ;
unsigned char r;
unsigned char dig [6];
dig[0] = 249;
dig[1] = 146;
dig[2] = 176;
dig[3] = 153;
dig[4] = 146;
dig[5] = 130;
/* Select the Register bank 1 */
set_bit ( STATUS, RP0 ) ;
/* set all of PORTB for output */
TRISB = 0x00 ;
/* set all of PORTA for input */
TRISA = 0x1f ;
/* now use Register bank 0 */
clear_bit ( STATUS, RP0 ) ;
// set all led's on
PORTB = 0;
// delay for 2 seconds
delay_s(2);
// set all LEDs off
PORTB = 255;
// Main program loop
r = 0;
while (1) {
// Wait for Switch A0
while ( key () != 1 );
while ( key () == 1 ) {
// generate a random number
r = r + 1;
if ( r == 6 )
r = 0;
}
// set all LEDs off
PORTB = 255;
// Rotate display 3 times
for ( i=0 ; i < 3 ; i= i + 1 ) {
for ( j=0 ; j < 6 ; j= j + 1 ) {
output_low_port_b ( j ) ;
if ( j == 0 ) {
output_high_port_b ( 5 ) ;
} else {
output_high_port_b ( j - 1 ) ;
}
delay_ms(100);
}
}
// Turn off led 5
output_high_port_b ( 5 ) ;
// Display number
PORTB = dig[r];
delay_s(1);
}
}
- - - Updated - - -
error at set_bit ( STATUS, RP0 ) ;