go_away

Author Topic: How: Declare a Port as (output or input) and give a digital value  (Read 1905 times)

0 Members and 1 Guest are viewing this topic.

Offline moltrex84Topic starter

  • Jr. Member
  • **
  • Posts: 27
  • Helpful? 0
Hello, ""Axon2"

**My question is simple how to declare in the hardware.h a port(B5) as output.

**How to give a digital value to that port in Myproject.C .

Example: how to turn a Led with a digital output, with a button as a digital input. ports(led:b5)(button:b7)
Declaration in .h   and logic in .c   ...


Most of my question in a near future are what method do I use, how they are call and what parameters they need.
Where I can find that kind of info ? I know the theory in circuits, DSP, communications ..., but I dont Know how to code it.
Is there a Big manual i can read ?



Thanks for your time, I wanna learn and be more guru with this microcontroller. :)

Offline moltrex84Topic starter

  • Jr. Member
  • **
  • Posts: 27
  • Helpful? 0
Re: How: Declare a Port as (output or input) and give a digital value
« Reply #1 on: September 01, 2010, 09:05:28 PM »
I have found a function list for the Axon 1 but I have a Axon 2 ... There I found:

DIGITAL OUTPUTS

All digital pins on the Axon are by default configured as digital outputs. So to change the output status, here are two examples of how to change pin A6.
PORT_ON(PORTA,6);

PORT_OFF(PORTA,6);
If it isn't configured as a digital output (because you changed the default), this code will configure pin A6.

DDRA = _BV(PA6);


Code: [Select]
// This is the main loop
TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart)
{
tempbyte=USB_GET;//get a single character

           if (tempbyte=='5')
            PORT_OFF(PORTB,6);
       if (tempbyte=='6')
          PORT_ON(PORTB,6);

return 2000; // wait for 20ms to stop crazy oscillations (long story)
}


But I'm getting an error ...  

----------------------------------------------------------------------
../Axon2_WebbotLib.c: In function 'appControl':
../Axon2_WebbotLib.c:108: warning: implicit declaration of function 'PORT_OFF'
../Axon2_WebbotLib.c:108: error: 'PORTB' undeclared (first use in this function)
../Axon2_WebbotLib.c:108: error: (Each undeclared identifier is reported only once
../Axon2_WebbotLib.c:108: error: for each function it appears in.)
../Axon2_WebbotLib.c:113: warning: implicit declaration of function 'PORT_ON'
make: *** [Axon2_WebbotLib.o] Error 1
Build failed with 3 errors and 2 warnings...

----------------------------------------------------------------------


How do a declare a PORT as Out..


Thanks,
« Last Edit: September 01, 2010, 09:07:55 PM by moltrex84 »

Offline moltrex84Topic starter

  • Jr. Member
  • **
  • Posts: 27
  • Helpful? 0
Re: How: Declare a Port as (output or input) and give a digital value
« Reply #2 on: September 04, 2010, 09:13:18 AM »
I still need help ...  ???

Offline KurtEck

  • Robot Overlord
  • ****
  • Posts: 217
  • Helpful? 12
Re: How: Declare a Port as (output or input) and give a digital value
« Reply #3 on: September 04, 2010, 03:39:18 PM »
I am a little rusty here,

But I believe that you are using webbotlib and webbotlib has a tendency to undefine a lot of the internal ARV stuff like portB.

Look at the webbotlib documention(pdf) and look at the section iopin.h

Here are a few extracts from my Lynxmotion biped brat code on webbotlib that shows a little of how to declare the ports and the like:
Code: [Select]
#define PS2_DAT K4
#define PS2_CMD K5
#define PS2_SEL K6
#define PS2_CLK K7
...

void PS2_Init(void)
{
    pin_make_input(PS2_DAT, TRUE);  // make the dat line input But also enable pull-up
pin_make_output(PS2_CMD,FALSE); // Make command pin be output
pin_make_output(PS2_SEL, TRUE); // init the select pin
//pin_high(PS2_SEL); // Initialize to high...
pin_make_output(PS2_CLK, FALSE); // Make clock be output
};
BYTE gameByte(BYTE command)
{
        short int i ;
short int j;
        delay_us(1);
        short int data = 0x00;                                   // clear data variable to save setting low bits later.
        for(i=0;i<8;i++)
        {
if (command & 0x1)
pin_high(PS2_CMD); //sbi(PORTK, PScommand);      // bit bang "command" out on PScommand wire.
            else
pin_low(PS2_CMD); //cbi(PORTK, PScommand);
            pin_low(PS2_CLK); //cbi(PORTK, PSclock);                             // CLOCK LOW
            //delay_us(1);              // wait for output to stabilise
for (j=10; j > 0; j--)
;
            if ( pin_is_high(PS2_DAT)) //if((PINK & _BV(PSdata)))
sbi(data, i);
            pin_high(PS2_CLK); //sbi(PORTK, PSclock);                             // CLOCK HIGH
command = command >> 1;
        }
        pin_high(PS2_CMD); //sbi(PORTK, PScommand);

//        delay_us(20);                                            // wait for ACK to pass.
for (j=100; j > 0; j--)
;


        return(data);
}
The above code shows setting up pins for input and output.  Likewise it shows setting values high and low and it shows getting the current value from an input pin.

I hope that helps.
Kurt

Offline moltrex84Topic starter

  • Jr. Member
  • **
  • Posts: 27
  • Helpful? 0
Re: How: Declare a Port as (output or input) and give a digital value
« Reply #4 on: September 05, 2010, 06:25:54 PM »
Hello,

KurtEck thanks for the reply. I saw your code and there are functions you used that I have not thought they exist and can get my code even cleaner.

------Digital outputs--------
I found how to do it. And Is very simple: 
 
appInitHardware:

pin_make_output(B6); // declare the B6 port as output

appControl

pin_high(B6);//on fan
delay_ms(5000);//Delay in ms ... 5000ms = 5seconds
pin_low(B6);//off fan


------Digital Inputs--------
Short story:
Here I was sneaking in the Photovore  example and saw the Button_pressed() ... I was like were does that function come from...
It was declare in axon2.h ... there you can see, it used the D5 as input ... Looking at the datasheet were is that D5 ??? ... Going back to the axon2.h in the definition of the DDRD = 0b11010000 all make sense it was a button next to the 7 segment plus it was documented in there ... in the button of the axon2.h I created a funtion just like the Button_pressed() but with a different name and a different port obviously and bingo.


in the axon2.h "Careful, if you do something really wrong here ... things will not work properly and you will need to download it again ..."

all the way down of that file 



// ---------------   Start: define Button on the board ---------------------
//    Original code by Admin @ www.societyofrobots.com

#define button_pressed() pin_is_low(D5)

#define ReadInputH7() pin_is_high(H7)  // You can declare as high or low ...The logic change thats all. "Active high or active low"
// ---------------   End: define Button on the board ---------------------

now in your code you do this:


if (!ReadInputH7())
{
       TheFamousDoSomethingFunction();
}




Now if you wanna see my complete code:

Video, Note -> "Is in Spanish"
Video0039.mp4


hardware.h :
Code: [Select]
//define servos, sensors, and other stuff here
//this keeps hardware separate from other code - making it easy to share/port

//declare libraries
#include "Sensors/Distance/Devantech/SRF05_Sonar.h"
#include "Sensors/Distance/Devantech/SRF04_Sonar.h"
#include "Sensors/Distance/Maxbotix/EZ1.h"
#include "Sensors/Distance/Ping/PingSonar.h"
#include "Sensors/Distance/Sharp/GP2.h"

//UART defines (name your UART)
#define GPS_UART UART0
#define USB_UART UART1
#define WIRELESS_UART UART2
#define OTHER_UART UART3
//UART baud defines (change baud rate)
#define GPS_BAUD (BAUD_RATE)9600
#define USB_BAUD (BAUD_RATE)115200
#define WIRELESS_BAUD (BAUD_RATE)38400
#define OTHER_BAUD (BAUD_RATE)38400
//UART define which uart to use
#define GPS_ACTIVATE &uart0SendByte
#define USB_ACTIVATE &uart1SendByte
#define WIRELESS_ACTIVATE &uart2SendByte
#define OTHER_ACTIVATE &uart3SendByte

#define USB_GET uart1GetByte()


Axon2_WebbotLib.c
Code: [Select]
// Example of using Axon for sonar and sharp IR

//uncomment the sensor you want to use
#define SONAR
//#define SHARP

#define RPRINTF_FLOAT

#include "sys/axon2.h"
#include "led.h"
#include "rprintf.h"
#include "hardware.h"




// This routine is called once only and allows you to do any initialisation
// Dont use any 'clock' functions here - use 'delay' functions instead
void appInitHardware(void){
// Set up the hardware UART to xmit the results
//initialize UART ports (see hardware.h to change baud/names)
uartInit(GPS_UART, GPS_BAUD); //UART0
uartInit(USB_UART, USB_BAUD); //USB
uartInit(WIRELESS_UART, WIRELESS_BAUD); //UART2
uartInit(OTHER_UART, OTHER_BAUD); //UART3



// Set rprintf to go to output
rprintfInit(USB_ACTIVATE);



//declare digital output pins

pin_make_output(B6);

pin_make_output(H6);



}

TICK_COUNT appInitSoftware(TICK_COUNT loopStart){
uartInit(USB_UART, USB_BAUD);
rprintf("\nc:\ message -> Axon initiated... MoltreX activated the security!!!\n\n   Security online in 5 Seconds \n\n");
delay_ms(1000);//button debounce delay
rprintf("\nc:\ message -> 4 seconds\n\n");
delay_ms(1000);//button debounce delay
rprintf("\nc:\ message -> 3 seconds\n\n");
delay_ms(1000);//button debounce delay
rprintf("\nc:\ message -> 2 seconds\n\n");
delay_ms(1000);//button debounce delay
rprintf("\nc:\ message -> 1 seconds\n\n");
delay_ms(1000);//button debounce delay
rprintf("\nc:\ message -> Alarm Active ! ... System Online ...\n\n");

        pin_high(B6);//Turning on the fan at the beginning
return 0;
}
int tempbyte;
// This routine is called repeatedly - its your main loop
TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart){



tempbyte=USB_GET;//get a single character

if (tempbyte=='1') //to reset back everything and kill the noisy alarm
{

pin_high(B6);//on fan
        pin_low(H6);//off Alarm

}


if (!ReadInputH7())//If the Ultra sonic sensor detects something
{
        rprintf("\nAlarm is On !!!.\n\n");
        pin_low(B6); //off fan
        pin_high(H6); //on alarm
}



return 2000;//add delay
}


With the Digital Output you can turn On everything. Just remember if is grater than 5VDC with 20mA use a relay and with the digital output you can trigger a bigger output and so on relays in cascade if you need to trigger something big.

With the digital input the same, just remember the rules of I/O with the Axon 2 ... you don't want to fry the pin.

Thanks, I order a sabertooth, a compass and other sonar sensor ... maybe more questions coming.



 


Get Your Ad Here

data_list