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.

programming in C help

Status
Not open for further replies.

janosandi

Full Member level 4
Joined
Jan 23, 2010
Messages
210
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,298
Activity points
2,788
programming in embedded C help

Hello Guys
i've moved to learning embedded C with help of a friend
but need some help with my project some ideas will be great
im using 4 7segment digits & sometimes i want to use 2 of them as a countdown counter & the others for display functions & in setting mode i want to use the 4 together to indicate
some functions like "open,Pro1,......"

any example of something similiar or any idea??

thx guys
John
 
Last edited:

Code:
#define DOT             SEGM[7]
const PIN_TypeDef DIG[4] = {GPIOA, GPIO_PIN_3,          //1
                            GPIOD, GPIO_PIN_6,          //2
                            GPIOD, GPIO_PIN_4,          //3
                            GPIOD, GPIO_PIN_1};         //4

const PIN_TypeDef SEGM[8] = {GPIOB, GPIO_PIN_5,         //A
                             GPIOB, GPIO_PIN_4,         //B
                             GPIOC, GPIO_PIN_4,         //C
                             GPIOC, GPIO_PIN_6,         //D
                             GPIOC, GPIO_PIN_7,         //E
                             GPIOA, GPIO_PIN_1,         //F
                             GPIOC, GPIO_PIN_3,         //G
                             GPIOC, GPIO_PIN_5};        //DP
Code:
void DISP_Handle (void)
{
    if (TIM2_IT_Flag)
    {
      static u8 DigN = 0;
      u8 cnt;
      TIM2_IT_Flag = 0;
      
      /* Turn off previos digit */
      if (DigN) PIN_OFF(DIG[DigN-1].GPIOx, DIG[DigN-1].PINx);     
      else PIN_OFF(DIG[sizeof(DIG)-1].GPIOx, DIG[sizeof(DIG)-1].PINx);
      
      /* Enable dot if needed */
      if (DigN == DotPos) PIN_OFF(DOT.GPIOx, DOT.PINx);
      else PIN_ON(SEGM[7].GPIOx, SEGM[7].PINx);   
      
      /* Turn on segments */
      for (cnt = 0; cnt != 7; cnt++) if ((1<<cnt) & SSEG_DIG[Value[DigN]]) 
        PIN_OFF(SEGM[cnt].GPIOx, SEGM[cnt].PINx);
      else
        PIN_ON(SEGM[cnt].GPIOx, SEGM[cnt].PINx);
      
      /* Turn on digit */
      if (DigEnabled[DigN])
        PIN_ON(DIG[DigN].GPIOx, DIG[DigN].PINx);
      
      /* Switch to next digit */
      if (DigN == sizeof(DIG)) DigN = 0;
      else DigN++;
    }
  
}
Code:
void uLongToDigArrarConvert (void)
{
  u32 temp = OutValue;
  Value[3] = temp % 10;
  temp /= 10;
  Value[2] = temp % 10;
  temp /= 10;
  Value[1] = temp % 10;
  temp /= 10;
  Value[0] = temp % 10;
}
 

thx for help
But im new to C so i couldnt understand anything hahahaha
anyway i need an idea to look for a solution for it
& BTW i have a problem if u use mikroC
in the examples there is ansel & anselh which as said disable all peripherals on ports
it gives me error when i try to compile any code usin ANSEL & ANSELH
other thing i've got stuck with interrupts & timer use
any good explained tutorial for this will b nice


thx
John
 

much easier to help when you see the schematics, know which mcu used, with compilier. I provided an example for STM8. Anyway, you should not get any throuble to write zero to ansel register to disable analog functions.
Actualy, only one thing you need is a datasheet. There are clearly explained how to work with registers and so on.
 

im just trying an example to unerstand timer & interrupt
but it gives me errors when i compile it
here is the code
Code:
/*Header******************************************************/
 
unsigned cnt; // Define variable cnt

void interrupt() {
 cnt++; // Interrupt causes cnt to be incremented by 1
 TMR0 = 96; // Timer TMR0 is returned its initial value
 INTCON = 0x20; // Bit T0IE is set, bit T0IF is cleared
}

void main() {
 OPTION_REG = 0x84; // Prescaler is assigned to timer TMR0
 ANSEL = 0; // All I/O pins are configured as digital
 ANSELH = 0;
 TRISB = 0; // All port B pins are configured as outputs
 PORTB = 0x0; // Reset port B
 TMR0 = 96; // Timer T0 counts from 96 to 255
 INTCON = 0xA0; // Enable interrupt TMR0
 cnt = 0; // Variable cnt is assigned a 0
 
 do { // Endless loop
 if (cnt == 400) { // Increment port B after 400 interrupts
 PORTB = PORTB++; // Increment number on port B by 1
 cnt = 0; // Reset variable cnt
 }
 } while(1);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/*Header******************************************************/
 
unsigned cnt; // Define variable cnt
 
void interrupt() {
 cnt++; // Interrupt causes cnt to be incremented by 1
 TMR0 = 96; // Timer TMR0 is returned its initial value
 INTCON = 0x20; // Bit T0IE is set, bit T0IF is cleared
}
 
void main() {
 OPTION_REG = 0x84; // Prescaler is assigned to timer TMR0
 ANSEL = 0; // All I/O pins are configured as digital
 ANSELH = 0;
 TRISB = 0; // All port B pins are configured as outputs
 PORTB = 0x0; // Reset port B
 TMR0 = 96; // Timer T0 counts from 96 to 255
 INTCON = 0xA0; // Enable interrupt TMR0
 cnt = 0; // Variable cnt is assigned a 0
 
 do { // Endless loop
 if (cnt == 400) { // Increment port B after 400 interrupts
 PORTB = PORTB++; // Increment number on port B by 1
 cnt = 0; // Reset variable cnt
 }
 } while(1);
}
 

Code:
[B]volatile[/B] unsigned cnt; // Define variable cnt
cnt should be declared as a volatile
 

Hi,

but it gives me errors when i compile it

If it gives you the errors, then give it to us.

Klaus
 

Code:
[B]volatile[/B] unsigned cnt; // Define variable cnt
cnt should be declared as a volatile

thx
i'll try it now
what the mean of Volatile
im new to C i used assembly before & tried to learn Basic then everyone told me tht C is more pro

- - - Updated - - -

its not working also
i'll make some search in the datasheet tonight thx
looking for ansel & anselh registers

thx

- - - Updated - - -

do i have to declare ANSEL & ANSELH ?
the error is undeclared identifier Or do i have to include any file ?
 

Special function registers like ANSEL or OPTION_REG are specific for a processor. With an industry standard C compiler like GNU C or Microchip XC8 you use header files to select the processor and define these objects. I'm not using mikroC, as far as I know, the processor specific definitions are automatically included when you select a processor in the project manager.

You can review the mikroC user manual for the meaning of volatile. As far as I understand it's not required with the specific usage of variable cnt. But in case of doubt, you should use it for all variables that are modified in interrupt and read in the main code.
 

I do not understand what you are trying to do with these statements:

Code:
 while(1);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 

I do not understand what you are trying to do with these statements:

Code:
 while(1);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

sorry by mistake

- - - Updated - - -

Hi,



If it gives you the errors, then give it to us.

Klaus

the error im talking about its
undeclred identifier 'ANSEL' in expression
undeclred identifier 'ANSELH' in expression

its working the port B without the ANSEL & ANSELH as digital output
i've used this as its included in the example of mikroC programming

- - - Updated - - -

Any good explained timer & interrupt routines will be great
what they mean by that the timer works in the back scenes i've used delay routines with assembly before & have included any inputs i want to check within the delay routine
how it works with timer & interrupts?
thx guys

John
 

ANSEL and ANSELH are registers in the PIC16F887 (as in the diagram) but my guess is you have selected a different processor in the compiler configuration. They do not exist in some other types of PIC so they may not be cross-referenced to an address if the wrong type was selected.

Brian.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top