Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Help With C Program please

Status
Not open for further replies.

GrandAlf

Advanced Member level 2
Joined
Mar 9, 2002
Messages
520
Helped
47
Reputation
92
Reaction score
6
Trophy points
1,298
Location
UK
Activity points
4,730
rtx51tny, watchdog

I have a bit of a problem, the following code works fine, but when I try to make a function out of the contents of the functions block, it compiles, but will not run.

Made a file called CancelCall.C
Added contents of the block marked FUNCTIONS
Included At898252,Delay and lcd Headers

Made file CancelCall.H
Added
Extern void CancelCall(void);

Added CancelCall.h to main file.

Compiles OK, but will not run. Still learning C, and have probably done something stupid. Any help greatly appreciated.

Sorry about taking up so much bandwidth.

=============================================

#include <AT898252.h>
#include <rtx51tny.h>
#include "lcd.h"
#include <stdio.h>
#include "delay.h"

// Bit Outputs
sbit led = P1^7;
sbit C1 = P2^6;
sbit C2 = P2^5;
sbit C3 = P2^4;
sbit Sounder = P3^2;
sbit BusyOp = P3^3;
sbit Tone = P3^4;
sbit Release = P3^5;
sbit AudVid = P3^6;
sbit Strobe = P3^7;

// Bit Inputs Active Low
sbit PTE = P2^7;
sbit TradeClock = P1^6;
sbit Privacy = P1^7;
sbit OpenDoor = P3^0;
sbit BusyIp = P3^1;

// Variables
unsigned char DialNumber , KeyRead;
unsigned int ActualNumber;
unsigned char Address;
unsigned char Loop;
char buf[8];

//FUNCTIONS
////////////////////////////////////////////////////////////////////////////////
void CancelCall(void) // Resets All Junction Boxes
{
unsigned char Address;
Strobe = 1;
Address = 0;
Tone = 0;
lcd_goto(0);
lcd_puts("END- Please Wait");
AudVid = 0; BusyOp = 1;
DelayMs(200);
Strobe = 0; DelayUs(50); Strobe = 1;
DelayMs(50);
while (Address < 255){
P0 = Address;
Strobe = 0; DelayUs(50); Strobe = 1;
DelayMs(20);
Address ++ ; }
lcd_goto(0);
lcd_puts("Dial Number ");
}
//////////////////////////////////////////////////////////////////////////////


job0 () _task_ 0 { // Main Startup Task
os_create_task (1); // Starts Ticker Routine + RTE

lcd_init();
DialNumber = 0;
KeyRead = 0;
ActualNumber = 0;
Sounder = 0;
CancelCall();

while (1) {
led = 0;
DelayMs (200);
led = 1;
DelayMs (200);
C1=0; C2=0; C3=0;
while ((P2 & 0x0F) == 0x0F) {} // Waits for Key Press
C1 = 0; C2 = 1; C3 = 1;
KeyRead = (P2 & 0x0F);
if (KeyRead == 14) DialNumber = 1;
if (KeyRead == 13) DialNumber = 4;
if (KeyRead == 11) DialNumber = 7;
if (KeyRead == 7) // Reset
{
DialNumber = 0;
KeyRead = 0;
ActualNumber = 0;
C1 = 1; C2 = 1; C3 = 1;
lcd_goto(0);
lcd_puts("Dial Number ");
}

C1 = 1; C2 = 0; C3 = 1;
KeyRead = (P2 & 0x0F);
if (KeyRead == 14) DialNumber = 2;
if (KeyRead == 13) DialNumber = 5;
if (KeyRead == 11) DialNumber = 8;
if (KeyRead == 7) DialNumber = 0;

C1 = 1; C2 = 1; C3 = 0;
KeyRead = (P2 & 0x0F);
if (KeyRead == 14) DialNumber = 3;
if (KeyRead == 13) DialNumber = 6;
if (KeyRead == 11) DialNumber = 9;
if (KeyRead == 7) { // Call Key this bit is wrong, should be call not cancel
ActualNumber = 0;
DialNumber = 0;
KeyRead = 0;
CancelCall(); }

ActualNumber = (ActualNumber * 10) + DialNumber;
if (ActualNumber > 0) {
lcd_clear();
lcd_puts("Dialling ");
lcd_goto(0x09);
sprintf(buf,"%d",ActualNumber);
lcd_puts(buf); }

C1 = 0; C2 = 0 ; C3 = 0;
while ((P2 & 0x0F) != 0x0F) {} // Waits for Key Release
os_wait (K_TMO,5,0); } }

//=====================================================
// Checks PTE and Opens Door Also Pulses Ext Watchdog
job1 () _task_ 1 {
Loop = 0; Release = 0;

while (1) {

while (PTE == 1) {
Sounder = 0; DelayMs(50); Sounder = 1; DelayMs(50); Sounder = 0;//change to 1 ms
os_wait (K_TMO,30,0); }

//needs sound statement here, when written
while (Loop <20) {
Release = 1;
Sounder = 0; DelayMs(50); Sounder = 1; DelayMs(50); Sounder = 0;//change to 1 ms
DelayMs(255);
Loop++; }
Loop = 0; Release = 0; } }
//=====================================================
 

use a led as a debug indicator !

If u don't have an ICE to debug you system
Use one of the led as a debug inducator i.g.

SET a led ON and OFF .

When you enter a function set the led ON and when
u live the function set it OFF

This whay U can know if you have done the function well or live on error

if you have an LCD you can use it better by sending remark to the LCD

bye



Bobi
 

Thanks Bobi

It has an Lcd, but is not doing anything. The routine works ok if in the main program, but not as a function. I think I do not fully understand how to make header files?.

Mike
 

Hi, GrandElf

If you are lerning C avoid when you can any multitasking OS. It's another big thing to learn.

I didn't undestand what you are trying to do. You moved CancelCall()
to another *.c module? If yes declare CancelCall() in you main module.

void CancelCall(void); // declares function , it's prototype of this function
// such line can be moved to header file
extern int externintvar; // declares external int variable

You can get big troubles if you avoid declaration's of external
functions.

Another danger can come from fact that number of task's multiplies
stack requirements. If one-task program requires forexample60 bytes of stack - 5tasks prog can require 300 bytes. Has Tiny enough ram
to hold this?

Good luck
 

Thanks Kef,

All I was trying to do was to make CancelCall a function. Really just to tidy up the main program. It does work fine where it is however.

I have not not declared ext variable in the header, maybe this is the problem?. I will try it.

Never found a satisfactory C guide that explains proerly how functions/headers work. I am not really sure if you should insert headers inside functions that use them, or weather the compiler will use those in the main body of the program.

In a lot of ways assembler is easier, in that you have control and understanding of what you are doing (hopefully).

Thx again for your advice, I will keep at it.

Mike
 

I think I may have caused a little confusion here. I am of course trying to make library files. lol

dont quite understand the connection between

.h and .c

Do they need to share the same filename?.
 

GrandAlf said:
I think I may have caused a little confusion here. I am of course trying to make library files. lol

dont quite understand the connection between

.h and .c

Do they need to share the same filename?.


No, they don't . Headers contain declaration's of variables and types and
declarations of functions(prototypes).
No obligations or special rules for headers.
You can declare everything when you need to.

If main.c makes calls to external modules you can declare external
functions/variables in main.c or move these lines to header.

Probably you don't see compiler warnings and may be errors. Turn them on if they disabled. Just start making compiler happy and you will learn.

--example1--
---main.c---
int a,b;

int func(int a)
{
return a*a;
}

void main(void)
{
a*=b;
a*=func(b);
}
-----------
--------------

Now move func and a to sub.c
--example2--
--sub.c----
int a;

int func(int a)
{
return a*a;
}
------------
---main.c---
// without header
extern int a;
int func(int a);

int b;

void main(void)
{
a*=b;
a*=func(b);
}
---------------

---example3
-- same sub.c

--- jklmn.h
extern int a;
int func(int a);
---------

---main.c
#include "jklmn.h"
int b;

void main(void)
{
a=func(10);
b=func(10);
}
 

Thanks again Kef,

Its all becoming clearer now.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top