Is there anybody who knows how to do this? This is part of a project that I am w
ID: 3555599 • Letter: I
Question
Is there anybody who knows how to do this? This is part of a project that I am working on for my Microcontroller System Design course, and I am in desperate need for help! The following flow chart is partly based on the Cypress Application Note AN2342 - "PSoC Calculator". Here's how it works:
The calculator must have the following features:
Write the Calculator C code by using the following Code Summary and Flow Chart:
PSoC 3 Code Summary for Calculator (Page 1):
Calculator C Code Flow Chart:
Additional Coding:
Explanation / Answer
//----------------------------------------------------------------------------
// C main line
//----------------------------------------------------------------------------
#include <m8c.h> // part specific constants and macros
#include "PSoCAPI.h" // PSoC API definitions for all User Modules
#include "calculator.h"
#include "lcd.h"
#include "keyboard.h"
#include "api.h"
char ret_prev;
char key_scan_code,key_pressed_flag;
#pragma interrupt_handler KeyBoard_Polling_ISR
void KeyBoard_Polling_ISR(){
char scan_row,i,key_pressed,ret_state;
signed char column,row;
KBD_PORT=KBD_SCAN_03_MASK; // initiate RET=0000; SCAN_03=1111
KBD_SCAN_4_PORT|=KBD_SCAN_4_MASK; // initiate SCAN_4 = 1
ret_state=KBD_PORT & KBD_RET_MASK; // read RET lines state
if (ret_prev==ret_state) return;
ret_prev=ret_state;
//SCAN rows
scan_row=KBD_SCAN_0_MASK; // set first row
key_pressed=0;
for (i=0;i<4;i++){
KBD_SCAN_4_PORT&=~KBD_SCAN_4_MASK; // reset SCAN_4 line
KBD_PORT=scan_row; // set row and reset RET lines
ret_state=KBD_PORT & KBD_RET_MASK; // read RET lines state
if (ret_state){
// key coodinates are determined
scan_row>>=4; // pressed key in the 1-4th row
key_pressed=1;
break;
}
if (i==3){ // all rows on 2 port are scanned, then try SCAN_4 line
KBD_PORT=0; // reset SCAN_03 and RET lines
KBD_SCAN_4_PORT|=KBD_SCAN_4_MASK; // set SCAN_4 line
ret_state=KBD_PORT & KBD_RET_MASK; // read RET lines state
if (ret_state){
scan_row=0x10; // pressed key in fifth row
key_pressed=1;
break;
}
}
scan_row<<=1; // set next row
}
// obtain KEY scan code
if (key_pressed){
row=column=-1;
while (ret_state){
ret_state>>=1;
column++;
};
while (scan_row){
scan_row>>=1;
row++;
};
key_scan_code = scan_codes_table[row][column]; // key scan code
key_pressed_flag=1; // set pressed flag for main process
}
}
void main()
{
signed char ch,s,d,e,vol;
int z,i;
M8C_EnableGInt ; // Turn on interrupts
LCM_Initial();
Cal_Show();
ret_prev=0;
key_pressed_flag=0;
M8C_EnableIntMask(INT_MSK0, INT_MSK0_SLEEP); // Enable Sleep Timer Interrupt Source
while(1){
if (key_pressed_flag){
Cal_Key_In(key_scan_code);
key_pressed_flag=0;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.