Actions

Difference between revisions of "Dual Display station - software description"

From Zenitel Wiki

(New page: == Coding conventions == === Software configuration: Using #define settings === By setting or removing #define precompiler statements, pieces of code can be easily added or removed. One ...)
 
Line 40: Line 40:
 
BYTE flash *string_example = "Hello World!";
 
BYTE flash *string_example = "Hello World!";
 
print_string( (BYTE *)(WORD)string_example );
 
print_string( (BYTE *)(WORD)string_example );
 +
 +
 +
== Code description ==
 +
 +
=== General program structure ===
 +
 +
After initialisation, the program enters an infinite loop in which all processes are executed. Some processes are executed every 10ms, while others run continuously.
 +
 +
To generate the 10ms intervals, TIMER0 interrupt is used. It increments a counter by 1 every 10ms. The main loop checks this counter, and executes the required processes if it is greater than 0. Also, the counter is decremented by 1.
 +
 +
The process states and timers are kept within the separate processes, and no high-level FSM is used. Global status flags are held in a bit-struct register "status".
 +
 +
=== Keyboard scanning ===
 +
 +
{|
 +
|-
 +
|width="100pt" |Located in file:
 +
|width="100pt" |<main.c>
 +
|-
 +
|Entry function: || scan_matrix_keyboard()
 +
|-
 +
|}
 +
 +
==== Hardware ====
 +
 +
The station keyboard is an 8x4 matrix connected to PB1-PD3 (outputs) and PD4-PD7 (inputs).
 +
 +
==== Debounce routine ====
 +
 +
For each of the 32 keys there is a debounce register of 8 bits:
 +
 +
{|
 +
|-
 +
|width="100pt" |* Key status:
 +
|width="100pt" |1 bit
 +
|-
 +
|* Key counter up:||3 bits
 +
|-
 +
|* Key counter down:||4 bits
 +
|-
 +
|}
 +
 +
The keyboard scanning process is executed by 10ms intervals. For a keypress to be accepted, the key must be held for a specified amount of 10ms counts, counted by the up-counter. The key code is then sent to the AlphaCom digit sending process. Similarly, a key release also requires the key to be released for some time, counted by the down-counter. The key code 0x80 is then sent, telling the digit process to stop sending.
 +
 +
==== Key codes ====
 +
 +
The 32 keys are mapped this way:

Revision as of 08:22, 14 August 2007

Coding conventions

Software configuration: Using #define settings

By setting or removing #define precompiler statements, pieces of code can be easily added or removed. One example of this is enabling or disabling all debug functionality in a release:

#define DEBUG_ENABLED

All such statements are collected in the top of the file <main.h>.

Loading C strings from FLASH

The IAR C compiler for AVR processors is by default configured for reading strings from DATA segment (RAM). This requires the compiler to copy the strings from FLASH to RAM during initialisation/cstartup. After that, the stings can be used in normal "ANSI C style":

printf("Hello World!");

Loading the strings to RAM is required for the strings to be manipulated during run-time. But by using special-designed functions instead of printf(), this can be avoided. Printing strings is done with a separate function, which loads text from FLASH only.

This could be achieved by pre-defining all strings:

BYTE flash *string_example = "Hello World!";

... and it would be used this way:

print_string(string_example);

This is not an easy code to read. Instead, the string could be used the normal way by making a few configurations:

  • The CSTR segment containing strings is configured as a CODE segment (FLASH) in the linker file <lnk3s4kb_MODIFIED.xcl>.
  • The function using the strings must cast the pointer from RAM to FLASH type. This is because the pointer is actually pointing at the correct address in FLASH, but the compiler still believes it is a RAM pointer. For example:

void print_string(BYTE *string_temp)

{
BYTE flash *string;
string = (BYTE flash *)(WORD)string_temp;
...
}

"string_temp" is an invalid RAM pointer, but the FLASH pointer "string" can now be used normally instead.

  • In some cases, pre-defined strings are preferred if a string is used multiple times. But now the problem is vice versa: The function is expecting a RAM pointer, but the pointer is in this case actually FLASH. So we have to cast the pointer to RAM to be able to compile the code:

BYTE flash *string_example = "Hello World!"; print_string( (BYTE *)(WORD)string_example );


Code description

General program structure

After initialisation, the program enters an infinite loop in which all processes are executed. Some processes are executed every 10ms, while others run continuously.

To generate the 10ms intervals, TIMER0 interrupt is used. It increments a counter by 1 every 10ms. The main loop checks this counter, and executes the required processes if it is greater than 0. Also, the counter is decremented by 1.

The process states and timers are kept within the separate processes, and no high-level FSM is used. Global status flags are held in a bit-struct register "status".

Keyboard scanning

Located in file: <main.c>
Entry function: scan_matrix_keyboard()

Hardware

The station keyboard is an 8x4 matrix connected to PB1-PD3 (outputs) and PD4-PD7 (inputs).

Debounce routine

For each of the 32 keys there is a debounce register of 8 bits:

* Key status: 1 bit
* Key counter up: 3 bits
* Key counter down: 4 bits

The keyboard scanning process is executed by 10ms intervals. For a keypress to be accepted, the key must be held for a specified amount of 10ms counts, counted by the up-counter. The key code is then sent to the AlphaCom digit sending process. Similarly, a key release also requires the key to be released for some time, counted by the down-counter. The key code 0x80 is then sent, telling the digit process to stop sending.

Key codes

The 32 keys are mapped this way: