Go Down

Topic: Setting up Register of Max32 Chipkit microcontroller (Read 2457 times) previous topic - next topic

Handy908

Hello,

I am new to Arduino and am not sure how to set up a register. Assuming I have an 8-bit register, called T0CON that I would like to set as 1000 1000 [in binary], can I just write T0CON = 1000 1000 ? Does the value need to be converted to decimal, or hex, or just leave it as binary?

Thanks

ron_sutherland

#1
Oct 03, 2016, 04:23 am Last Edit: Oct 03, 2016, 04:36 am by ron_sutherland
chipKIT? that is the pic32-gcc stuff isn't it, so I think you will not have 8-bit registers. TOCON looks like a PIC18 register (from a quick google search).

Anyway if the toolchain has defined TOCON then a value can be assigned to it.

The AVR toolchain has the ADCSRA register defined, it has also defined the bits that can be set in it (ADEN, ADATE, and ADIE). It is fairly common practice to use those bit definitions in a way that documents the registers intended configuration.

Code: [Select]
ADCSRA = ADCSRA | (1<<ADEN);
ADCSRA = ADCSRA & ~(1<<ADATE);
ADCSRA = ( (ADCSRA | (1<<ADEN) ) & ~(1<<ADATE) ) | (1 << ADIE);


ADEN is the bit position and 1<<ADEN will shift a 1 into that position which can be used with bitwise-or to set the bit.  ADATE is another bit position and ~( 1<<ADATE) will clear it and set everything else to one which can be used with bitwise-and to clear the bit. They can also be combined, or you can assign a hex (e.g. 0x88) or binary literal (e.g. 0b1000100 in gcc anyway) and cause future you to look up their meanings again and again :-)
my projects: https://github.com/epccs

DrAzzy

10001000 is 10 million one thousand. Obviously that would get truncated if it's being stuffed into an 8-bit register - clearly not what you want.

0b10001000, or 0x88 or 134 (if I did the math right) will all be the same, and what you want.

Also - what does this have to do with Arduino?
ATTinyCore for x4/x5/x61/x7/x8/x41/1634/828/x313 megaTinyCore for the megaavr ATtinies - Board Manager:
http://drazzy.com/package_drazzy.com_index.json
ATtiny breakouts, mosfets, awesome prototyping board in my store http://tindie.com/stores/DrAzzy

ron_sutherland

#3
Oct 03, 2016, 09:02 pm Last Edit: Oct 03, 2016, 09:09 pm by ron_sutherland
Quote
what does this have to do with Arduino?
chipKIT looks like a fork to me, and sometimes they get folded back into the main project (it is not common). Also, the PIC32  (e.g. MIPS) seems like it is a solid MCU for use with GCC. The other PIC devices have the wrong insides for use with GCC.
my projects: https://github.com/epccs

bperrybap

Also - what does this have to do with Arduino?
"Arduino" doesn't necessarily mean an arduino.cc or arduino.org product.
Chipkit offers an arduino environment for the pic32 processors.
While it used to require a fork of the IDE called MPIDE, the chipkit core can now be installed and used on recent IDEs using the board manager.
http://chipkit.net/wiki/index.php?title=ChipKIT_core

The pic32 has lot more bang for the buck than the AVR and is available in DIP packages.
Here is a write up for how to use it on a bread board:
http://chipkit.net/tag/breadboard/

--- bill


Handy908

Thanks for the reply guys.

@Ron sutherland:
I tried to insert the ADCSRA = ADCSRA | (1<<ADEN); under void setup()
However, I got error message saying 'ADCSRA' is not declared in this scope.
So I tried to include some libraries on the very top line of my codes:
#include "Bridge"
#include "p32mx795f512l.h"
#include "xc.h"
#include "stdio.h"

But apparently, this is not the right way to invoke library. Do you have any idea how to fix this issue?

westfw

Quote
I tried to insert the ADCSRA = ADCSRA | (1<<ADEN); under void setup()
However, I got error message saying 'ADCSRA' is not declared in this scope.
Well of course.  ADCSRA is the name of a peripheral register on the AVRs.  On the PIC32, you have entirely different peripherals from a different vendor, perhaps addressed in a different way ("ADC0->status" wouldn't surprise me.)

To address the peripherals at the register level, you'll need to become familiar with those peripherals by reading the chip datasheet(s) and related documentation.  (NOT having to do this is one of the big advantages of an Arduino-like environment.)   Here's some sample code I found in the pic32 cores/wiring_analog.c file.

A2D Converter settings from "analogRead()":
 
Code: [Select]
AD1PCFG = ~(1 << channelNumber);
 AD1CHS = (channelNumber & 0xFFFF) << 16;
 AD1CON1 = 0; //Ends sampling, and starts converting

 //Set up for manual sampling
 AD1CSSL = 0;
 AD1CON3 = 0x0002; //Tad = internal 6 Tpb
 AD1CON2 = analog_reference;


Timer code from analogWrite():

Code: [Select]
            // If no PWM are currently active, then init Timer2
            if (pwm_active == 0)
            {
                T2CON = T2_PS_1_256;
                TMR2 = 0;
                PR2 = PWM_TIMER_PERIOD;
                T2CONSET = T2_ON;
           }


(If you're looking to do something specific, you might be able to get answers to questions  like "how do I change the frequency used by digitalWrite() PWM on pin X?", but otherwise you're definitely in "read the datasheet" territory.)


westfw

Quote
Quote
Also - what does this have to do with Arduino?
Chipkit offers an arduino environment for the pic32 processors.
Chipkit boards DO have their own forum, here: http://chipkit.net/forum/

ron_sutherland

Yep the ADCSAR is for AVR, sorry about that, it is all I really know at this point. But you can now look for some of those registers westfw has listed inside that p32mx795f512l.h file.

https://raw.githubusercontent.com/jasonkajita/pic32-part-support/master/include/proc/p32mx795f512l.h

Looking in that I see AD1PCFG  has bit PCFG0 defined so things like
AD1PCFG  = AD1PCFG  | (1<<PCFG0); should compile.

If you look at the datasheet it should tell you about the AD1PCFG register and the PCFG0 bit does. This is the first I've looked at these so I'm not going to be much help.
my projects: https://github.com/epccs

Go Up