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.

How to use TIMER function on CodeVision

Status
Not open for further replies.

faeng

Newbie level 6
Newbie level 6
Joined
Apr 27, 2008
Messages
12
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Thailand
Activity points
1,433
I'm new for CodeVision and I want to know to write TIMER ofunction.
what's source code ?
 

I use codeVision alot.
I don't understand what you want to do exactly.

What is the timer function you want to write?

regards
 

    faeng

    Points: 2
    Helpful Answer Positive Rating
I doing Pulse Oximeter project and
I want to seek maximum value and minimum value of wave

I don't know how to setting Timer and don't know source code to write

Thank you for you comment :)
 

Look if you can just explain your project so I can help you.

Like if you can just write down in steps what the microcontroller should do.

regards

Added after 52 seconds:

Another thing about using Codevision

You can use the project generation tool to setup the timer.
 

My project is monitoring percentage of oxygen that is saturated with hemoglobin ; it measures blood oxygenation by sensing the Infrared and Red light absorption
properties of deoxygenated and oxygenated hemoglobin.

It consist of a sensing probe attached to a patient's finger that is connectec to a system for calculation and display of the percentage of oxygen saturated with hemoglobin.
I use sensor of Nellcor company and show percen of oxygen by LCD.

The microcontroller for the oximeter needed to perform the following tasks:
· control the sensor LEDs
· convert the sensor output into a digital form
· receive value from A/D to calculate and show on LDC
 

Steps of microcontrollor

The first

Microcontroller must control LEDs Driver and then signal from sensor into Input Amplifier and then signal from input amplifier into Syncronous detector and Demodulator for part IR and R
 

step 2

- A/D receive analog signal and convert to digital IR and R to maximum
- Compar of samping to 5 value for maximum
- Clear Max and Min to samping
- seek maximum value
- Analog to Digital Converter IR and R to minimum
- Compar of Samping to 5 value for minimum
- seek minimum value
 

faeng said:
step 2

- A/D receive analog signal and convert to digital IR and R to maximum
- Compar of samping to 5 value for maximum
- Clear Max and Min to samping
- seek maximum value
- Analog to Digital Converter IR and R to minimum
- Compar of Samping to 5 value for minimum
- seek minimum value

I tried to figure out the relation between the timer and what you mentioned here.

What do you mean exactly by " A/D receive analog signal and convert to digital IR and R to maximum" I don't get what is the maximum and minimum here.

Regards
 

I have pulse like this and I want to know how to know maximum ann minimum value. How I write program? That make I know T


Thank for your comment
 

ok,
I finally understand what you wanna do.

You take a varying analog signal and you want to know the maxima's and the minima's and want to know the time between them.

ok, I have a couple of questions for you before I can give you a solution. How long do u expect T will be?
in the range of ms, us, seconds or mins ...

Which AVR chip do u use?

please check http://www.atmel.com/dyn/resources/prod_documents/doc2502.pdf

This the datasheet for the chip ATMEGA8535.

In this datasheet, search for "Real Time". You will find how to make a real time counter and how you can control it.

The need for real time counter depends on the range of T.

Regards
 

That right :D
I expect T will be mins in the range . I use AVR ATmega32.
And this datasheet for the chip ATMEGA8535.


**broken link removed**
 

Hi,

Here is what you wanna do :

You will setup the timer2 on the chip to real timer, refer to the datasheet page 131 and read about the values for the register to setup the timer as a real time ok.

In codevision, you will setup the timer as an external Osc and you will add the 32K crystal on pins indicated in the datasheet page 131 on port C. Set the prescaler to the maximum value so that the counting cycles aren't too much.

Now in the beginning of the program. Disable the timer, and poll on the input from the ADC and read the values make a code that search for maximum and minimum. At first, search for maximum, when maximum is reached enable the timer. You might want to set the timer to interrupt at overflow for large time counting. Then continue polling until you find the next maximum at this time check the value of the timer. You now have calculated the first T.

Do the same for minima's as maxima's.


You might need to define a state bits (global bits) to control the flow of your program.

Regards
 

    faeng

    Points: 2
    Helpful Answer Positive Rating
thank you for your help and I have some question that's
I want to know how I write this code ?

plaese hints to me again

thank you
 

First of all, Do you know how to program in C?

If you know, Then here is what you want do:

1- Use the code generation wizard in code vision to create the initialization for you.
Setup the timer as we discussed. But change initialization so it is stopped until you find a maximum.

2- Create a function for getting the minimum and other for maximum.

3- Call these functions to check for minima or maxima and start the timer at minima or maxima.

That's all.

Please try these steps and send me the code and then we can discuss more.

Regards
 

This is my program but it's not work








/*****************************************************
This program was produced by the
CodeWizardAVR V1.24.6 Professional
Automatic Program Generator
ฉ Copyright 1998-2005 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com
e-mail:eek:ffice@hpinfotech.com

Project :
Version :
Date : 15/5/2008
Author : F4CG
Company : F4CG
Comments:


Chip type : ATmega32
Program type : Application
Clock frequency : 14.745600 MHz
Memory model : Small
External SRAM size : 0
Data Stack size : 512
*****************************************************/

#include <mega32.h>
#include <delay.h>
#include <stdio.h>
#define SAMPLING 5
unsigned int read_adc(unsigned char adc_input);
register static unsigned char input_index=0;
register static unsigned int loop;
unsigned char i,j,k,p;
unsigned int Vmax, Vmin;
unsigned int sampling_data1[SAMPLING];
unsigned int sampling_data2[SAMPLING];
unsigned s;
char str[16];

// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0x15 ;PORTC
#endasm
#include <lcd.h>

// Timer 2 overflow interrupt service routine
interrupt [TIM2_OVF] void timer2_ovf_isr(void)
{
if (input_index<SAMPLING){
sampling_data1[input_index]=ADCW;
// Place your code here

}}

#define ADC_VREF_TYPE 0x00

// Read the AD conversion result
unsigned int read_adc(unsigned char adc_input)
{
ADMUX=adc_input|ADC_VREF_TYPE;
// Start the AD conversion
ADCSRA|=0x40;
// Wait for the AD conversion to complete
while ((ADCSRA & 0x10)==0){};
ADCSRA|=0x10;
sampling_data1[input_index]=ADCW;
if (input_index == 0) { // Initial
Vmax = ADCW;
Vmin = ADCW;
}
if (ADCW > Vmax) {Vmax = ADCW;}
if (ADCW < Vmin) {Vmin = ADCW;}
//input_index++;
//loop = 0;
else {loop++;}
TCNT0=0xFB;
return ADCW;
}


// Declare your global variables here

void main(void)
{
// Declare your local variables here

// Reset Source checking
if (MCUCSR & 1)
{
// Power-on Reset
MCUCSR=0;
// Place your code here

}
else if (MCUCSR & 2)
{
// External Reset
MCUCSR=0;
// Place your code here

}
else if (MCUCSR & 4)
{
// Brown-Out Reset
MCUCSR=0;
// Place your code here

}
else
{
// Watchdog Reset
MCUCSR=0;
// Place your code here

};

// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTA=0x00;
DDRA=0x00;

// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0x00;

// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;

// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Phase correct PWM top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x40;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x40;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// ADC initialization
// ADC Clock frequency: 115.200 kHz
// ADC Voltage Reference: AREF pin
ADMUX=ADC_VREF_TYPE;
ADCSRA=0x87;

// LCD module initialization
lcd_init(16);

// Watchdog Timer initialization
// Watchdog Timer Prescaler: OSC/32k
WDTCR=0x09;

// Global enable interrupts
#asm("sei")

while (1)
{
sampling_data1[input_index] = read_adc(0);
delay_ms(700);
}
lcd_gotoxy(0,0);
sprintf(str,"%u",Vmin);
lcd_puts(str);
lcd_gotoxy(4,0);
sprintf(str,"%u",Vmax);
lcd_puts(str);
// Place your code here


}
 

This is my new code to veed mix and min it work but I don't know
how to veed my pulse that I sent to you














/*****************************************************
This program was produced by the
CodeWizardAVR V1.24.6 Professional
Automatic Program Generator
? Copyright 1998-2005 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com
e-mail:eek:ffice@hpinfotech.com

Project :
Version :
Date : 15/5/2008
Author : F4CG
Company : F4CG
Comments:


Chip type : ATmega32
Program type : Application
Clock frequency : 11.059200 MHz
Memory model : Small
External SRAM size : 0
Data Stack size : 512
*****************************************************/

#include <mega32.h>
#include <delay.h>
#include <stdio.h>
#include <math.h>


// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0x15 ;PORTC
#endasm
#include <lcd.h>
#define SAMPLING 5
unsigned int read_adc(unsigned char adc_input);
register static unsigned int input_index;
register static unsigned int loop;
unsigned int i,j;
unsigned int k,p,l,m,n,o;
int sampling_data1[SAMPLING];
int sampling_data2[SAMPLING];
int sampling_data3[SAMPLING];
int sampling_data4[SAMPLING];
unsigned int s;
int str[16];
// Timer 0 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
// Reinitialize Timer 0 value
TCNT0=0xFE;
// Place your code here
sampling_data1[input_index] = read_adc(0);
sampling_data3[input_index] = read_adc(0);

/* lcd_clear();
lcd_gotoxy(12,1);
sprintf(str,"%u",sampling_data1[input_index]);
lcd_puts(str); */
input_index++;
}
#define ADC_VREF_TYPE 0xC0

// Read the AD conversion result
unsigned int read_adc(unsigned char adc_input)
{
ADMUX=adc_input|ADC_VREF_TYPE;
// Start the AD conversion
ADCSRA|=0x40;
// Wait for the AD conversion to complete
while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCW;
}

// Declare your global variables here
unsigned int s;

void main(void)
{
// Declare your local variables here

// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTA=0x00;
DDRA=0x00;

// Port B initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
PORTB=0x00;
DDRB=0xFF;

// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;

// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 172.800 kHz
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x05;
TCNT0=0xFE;
OCR0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x01;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// ADC initialization
// ADC Clock frequency: 172.800 kHz
// ADC Voltage Reference: Int., cap. on AREF
ADMUX=ADC_VREF_TYPE;
ADCSRA=0x86;

// LCD module initialization
lcd_init(16);

// Global enable interrupts
#asm("sei")
while(1)
{

while(input_index >= SAMPLING)
{
#asm("cli") //when received 5 samples--clear interrupt

k = sampling_data1[1];
p = sampling_data1[2];
j = max(k,p);
sampling_data2[1] = j;

l = sampling_data3[1];
m = sampling_data3[2];
n = max(l,m);
sampling_data4[1] = n;

o = max(j,n);

p = o;


lcd_clear();
lcd_gotoxy(7,1);
sprintf(str,"%u",o);
lcd_puts(str);

lcd_gotoxy(5,0);
sprintf(str,"%u",j);
lcd_puts(str);

lcd_gotoxy(10,0);
sprintf(str,"%u",n);
lcd_puts(str);

j=0;
delay_ms(400);
lcd_clear();
input_index=0;
#asm("sei") // when do all things--enable interrupt
};
}
}
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top