Hi, please i need to fix this code for my microprocessor project. below is the c
ID: 656741 • Letter: H
Question
Hi, please i need to fix this code for my microprocessor project. below is the code and the error showing. its telling me to define the get_btn. how do i define the get_btn. Thanks
Program:
#include "C:UsersIsmailDocumentsDocumentsSourceshcs12.h"
#include "C:UsersIsmailDocumentsDocumentsSourcesdelay.h"
#include "C:UsersIsmailDocumentsDocumentsSourceslcd.h"
#include "C:UsersIsmailDocumentsDocumentsSourceslcd_util.h"
#define SW2 !(PTH & 0x08)
#define SW3 !(PTH & 0x04)
#define SW4 !(PTH & 0x02)
#define SW5 !(PTH & 0x01)
#define ONE 0x06
#define TWO 0x5B
#define THREE 0x4F
#define FOUR 0x66
#define FIVE 0x6D
void SetClk8(void);
unsigned int level_delay[] = {4000,3000,2000,1000,500};
unsigned char level;
unsigned char select_level(void);
void play(unsigned char level);
char scanKeyPad(void);
unsigned char get_btn(void);
void LCD_stat_Msg(char* message, unsigned char count1);
void LCD_vari_Msg(char* before, unsigned char var, char* after);
void display_LED(char num, char level);
//globals
char StartMsg[] = "Memory Pattern Game. Press SW2";
char LevelMsg[] = "Enter Level and press SW3";
char LevelMsg1[] = "Stage ";
char LevelMsg2[] = "Began" ;
char LevelMsg3[] = "Try Again. Press SW2";
char LevelMsg4[] = "Level ";
char LevelMsg5[] = "done. Press SW2";
void main(void)
{
unsigned int x = 0;
SetClk8();
DDRH = 0;
PTA = 0;
DDRA = 0x0F;
PTB = 0;
DDRB = 0xFF;
PTP = ~0x08;
DDRP =0xFF;
LCD_stat_Msg(StartMsg,0);
while(!SW2);
while(1) {
level = select_level();
play(level);
}
}
// -------------------------------------------------------------------
// This function enables PLL and use an 8-MHz crystal oscillator to
// generate 24-MHz E clock.
// -------------------------------------------------------------------
void SetClk8 (void) {
SYNR = 0x02; // set SYSCLK to 24 MHz from a 4-MHz oscillator
REFDV = 0; // "
PLLCTL = 0x60; // turn on PLL, set automatic
while(!(CRGFLG & LOCK));
CLKSEL |= PLLSEL; // clock derived from PLL
}
unsigned char select_level(void)
{
unsigned char key;
unsigned char level = 1;
PTB = ONE;
LCD_stat_Msg(LevelMsg,0);
while(!SW3)
{
key = scanKeyPad();
switch(key)
{
case 1: {
PTB = ONE;
level = key;
break;
}
case 2:{
PTB = TWO;
level = key;
break;
}
case 3:{
PTB = THREE;
level = key;
break;
}
case 4:{
PTB = FOUR;
level = key;
break;
}
case 5:{
PTB = FIVE;
level = key;
break;
}
default:{
break;
}
}
delayby1ms(10);
}
return level;
}
void play(unsigned char level)
{
unsigned char x;
unsigned char stage = 1;
unsigned char LEDseq[8];
unsigned char MATCHseq[8] = {0,0,0,0,0,0,0,0};
unsigned char match = 1;
for(x = 0;x<4; x++){
LEDseq[x] = (rand() % 4+1);
}
while(match)
{
LCD_vari_Msg(LevelMsg1, stage, LevelMsg2);
for(x = 0; x<(stage + 3); x++)
{
display_LED(LEDseq[x],level);
delayby100ms(10);
}
for(x = 0; x<(stage + 3); x++)
{
MATCHseq[x]=get_btn();
}
for(x = 0; x<(stage + 3);x++)
{
if(MATCHseq[x] != LEDseq[x])
{
match = 0;
PTP = 0xff;
PTJ &= ~0x02;
PTB = 0xFF;
LCD_stat_Msg(LevelMsg3,0);
while(SW2 || SW3 ||SW4 || SW5);
while(!SW2);
PTJ |= 0x02;
PTB = 0;
PTP = ~0x08;
delayby1ms(1);
break;
}
}
if(match)
{
stage++;
if(stage == 6) {
LCD_vari_Msg(LevelMsg4, level, LevelMsg5);
while(!SW2);
match = 0;
}
else{
LEDseq[stage + 2] = (rand() % 4+1);
}
}
delayby100ms(10);
}
}
void display_LED(char num, char level)
{
unsigned int x;
unsigned char seg7set = PTB;
unsigned char LEDset = 1<<(num+3);
unsigned int delay = level_delay[level-1]/20;
DDRJ |= 0x02;
for(x = 0; x < delay; x++) {
PTP = 0xFF;
PTJ &= ~0x02;
PTB = LEDset;
delayby1ms(10);
PTJ |= 0x02;
PTB = seg7set;
PTP = ~0x08;
delayby1ms(10);
}
}
void LCD_stat_Msg(char* message, unsigned char count1)
{
char count = 0 + count1;
if(count == 0)
{
openLCD();
}
while(*message != '')
{
LCDData(*message);
message++;
count++;
if(count == 16) {
LCDControl(0xc0);
delayby1ms(1);
}
}
}
void LCD_vari_Msg(char *before, unsigned char var, char *after)
{
unsigned char length1 = 0;
unsigned char* ptr1 = before;
while(*ptr1 != '') {
length1++;
ptr1++;
}
LCD_stat_Msg(before,0);
putcLCD(var + '0');
LCD_stat_Msg(after, length1+1);
}
char scanKeyPad(void)
{
char pressed;
PTA = 0x08;
pressed = (PTA >> 4);
PTA = 0x00;
switch(pressed)
{
case 8:
return 'D';
case 4:
return 'C';
case 2:
return 'B';
case 1:
return 'A';
}
PTA = 0x04;
pressed = (PTA >> 4);
PTA = 0x00;
switch(pressed)
{
case 8:
return '#';
case 4:
return 9;
case 2:
return 6;
case 1:
return 3;
}
PTA = 0x02;
pressed = (PTA >> 4);
PTA = 0x00;
switch(pressed)
{
case 8:
return 0;
case 4:
return 8;
case 2:
return 5;
case 1:
return 2;
}
PTA = 0x01;
pressed = (PTA >> 4);
PTA = 0x00;
switch(pressed)
{
case 8:
return '*';
case 4:
return 7;
case 2:
return 4;
case 1:
return 1;
default:
return -1;
}
}
Error:
Link Error : L1822: Symbol get_btn in file C:UsersIsmailDocumentsProject_4Project_4_DataStandardObjectCodemain.c.o is undefined
Explanation / Answer
#include<stdio.h>
#include <conio.h>
#define SW2 !(PTH & 0x08)
#define SW3 !(PTH & 0x04)
#define SW4 !(PTH & 0x02)
#define SW5 !(PTH & 0x01)
#define ONE 0x06
#define TWO 0x5B
#define THREE 0x4F
#define FOUR 0x66
#define FIVE 0x6D
void SetClk8(void);
unsigned int level_delay[] = {4000,3000,2000,1000,500};
unsigned char level;
unsigned char select_level(void);
void play(unsigned char level);
char scanKeyPad(void);
unsigned char get_btn(void);
void LCD_stat_Msg(char* message, unsigned char count1);
void LCD_vari_Msg(char* before, unsigned char var, char* after);
void display_LED(char num, char level);
char StartMsg[] = "Memory Pattern Game. Press SW2";
char LevelMsg[] = "Enter Level and press SW3";
char LevelMsg1[] = "Stage ";
char LevelMsg2[] = "Began" ;
char LevelMsg3[] = "Try Again. Press SW2";
char LevelMsg4[] = "Level ";
char LevelMsg5[] = "done. Press SW2";
void main(void)
{
unsigned int x = 0;
SetClk8();
DDRH = 0;
PTA = 0;
DDRA = 0x0F;
PTB = 0;
DDRB = 0xFF;
PTP = ~0x08;
DDRP =0xFF;
LCD_stat_Msg(StartMsg,0);
while(!SW2);
while(1) {
level = select_level();
play(level);
}
}
void SetClk8 (void) {
SYNR = 0x02;
REFDV = 0;
PLLCTL = 0x60;
while(!(CRGFLG & LOCK));
CLKSEL |= PLLSEL;
}
unsigned char select_level(void)
{
unsigned char key;
unsigned char level = 1;
PTB = ONE;
LCD_stat_Msg(LevelMsg,0);
while(!SW3)
{
key = scanKeyPad();
switch(key)
{
case 1: {
PTB = ONE;
level = key;
break;
}
case 2:{
PTB = TWO;
level = key;
break;
}
case 3:{
PTB = THREE;
level = key;
break;
}
case 4:{
PTB = FOUR;
level = key;
break;
}
case 5:{
PTB = FIVE;
level = key;
break;
}
default:{
break;
}
}
delayby1ms(10);
}
return level;
}
void play(unsigned char level)
{
unsigned char x;
unsigned char stage = 1;
unsigned char LEDseq[8];
unsigned char MATCHseq[8] = {0,0,0,0,0,0,0,0};
unsigned char match = 1;
for(x = 0;x<4; x++){
LEDseq[x] = (rand() % 4+1);
}
while(match)
{
LCD_vari_Msg(LevelMsg1, stage, LevelMsg2);
for(x = 0; x<(stage + 3); x++)
{
display_LED(LEDseq[x],level);
delayby100ms(10);
}
for(x = 0; x<(stage + 3); x++)
{
MATCHseq[x]=get_btn();
}
for(x = 0; x<(stage + 3);x++)
{
if(MATCHseq[x] != LEDseq[x])
{
match = 0;
PTP = 0xff;
PTJ &= ~0x02;
PTB = 0xFF;
LCD_stat_Msg(LevelMsg3,0);
while(SW2 || SW3 ||SW4 || SW5);
while(!SW2);
PTJ |= 0x02;
PTB = 0;
PTP = ~0x08;
delayby1ms(1);
break;
}
}
if(match)
{
stage++;
if(stage == 6) {
LCD_vari_Msg(LevelMsg4, level, LevelMsg5);
while(!SW2);
match = 0;
}
else{
LEDseq[stage + 2] = (rand() % 4+1);
}
}
delayby100ms(10);
}
}
void display_LED(char num, char level)
{
unsigned int x;
unsigned char seg7set = PTB;
unsigned char LEDset = 1<<(num+3);
unsigned int delay = level_delay[level-1]/20;
DDRJ |= 0x02;
for(x = 0; x < delay; x++) {
PTP = 0xFF;
PTJ &= ~0x02;
PTB = LEDset;
delayby1ms(10);
PTJ |= 0x02;
PTB = seg7set;
PTP = ~0x08;
delayby1ms(10);
}
}
void LCD_stat_Msg(char* message, unsigned char count1)
{
char count = 0 + count1;
if(count == 0)
{
openLCD();
}
while(*message != '')
{
LCDData(*message);
message++;
count++;
if(count == 16) {
LCDControl(0xc0);
delayby1ms(1);
}
}
}
void LCD_vari_Msg(char *before, unsigned char var, char *after)
{
unsigned char length1 = 0;
unsigned char* ptr1 = before;
while(*ptr1 != '') {
length1++;
ptr1++;
}
LCD_stat_Msg(before,0);
putcLCD(var + '0');
LCD_stat_Msg(after, length1+1);
}
char scanKeyPad(void)
{
char pressed;
PTA = 0x08;
pressed = (PTA >> 4);
PTA = 0x00;
switch(pressed)
{
case 8:
return 'D';
case 4:
return 'C';
case 2:
return 'B';
case 1:
return 'A';
}
PTA = 0x04;
pressed = (PTA >> 4);
PTA = 0x00;
switch(pressed)
{
case 8:
return '#';
case 4:
return 9;
case 2:
return 6;
case 1:
return 3;
}
PTA = 0x02;
pressed = (PTA >> 4);
PTA = 0x00;
switch(pressed)
{
case 8:
return 0;
case 4:
return 8;
case 2:
return 5;
case 1:
return 2;
}
PTA = 0x01;
pressed = (PTA >> 4);
PTA = 0x00;
switch(pressed)
{
case 8:
return '*';
case 4:
return 7;
case 2:
return 4;
case 1:
return 1;
default:
return -1;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.