Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 12:32 15 May 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Machine code for SetPin 2,din

     Page 3 of 3    
Author Message
rogersan
Regular Member

Joined: 10/04/2015
Location: Australia
Posts: 79
Posted: 02:58am 12 May 2015
Copy link to clipboard 
Print this post

I notice that the port number and PIN number are used as pointers. So to call this function I would have to define pointers in the main program (that uses the function) and set the locations pointed to to the port and PIN numbers.

This seems unnecessary as just using integer variables for port and PIN numbers would be OK.
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 8601
Posted: 02:59am 12 May 2015
Copy link to clipboard 
Print this post

What port number and pin number are you using in the call to the Cfunction? It would be helpful if you post the complete program Basic and C in a zip
 
G8JCF

Guru

Joined: 15/05/2014
Location: United Kingdom
Posts: 676
Posted: 03:24am 12 May 2015
Copy link to clipboard 
Print this post

@RogerSan

In your MMBasic program, you just use ordinary variables, eg



Dim PortNumber%=1
Dim PinNumber%=2
Dim as Integer Log(2048)

Dim Result%

Result%=dht11convppm(PortNumber%, PinNumber%,Log())



The addresses of these regular MMBasic variables are what are passed to the CFunction as pointers.

Peter
The only Konstant is Change
 
G8JCF

Guru

Joined: 15/05/2014
Location: United Kingdom
Posts: 676
Posted: 03:31am 12 May 2015
Copy link to clipboard 
Print this post

@RogerSan

I've looked at the disassembly with -O1, and it would seem that the exception is being generated at


mytmr1con=0x8030; //turn on with scale 256
9d000050: 34078030 li a3,0x8030
9d000054: 3c04f800 lui a0,0xf800
9d000058: ac876000 sw a3,24576(a0)


The problem is with your definition of the macro


#define mytmr1con *(volatile unsigned int *)0xbf8006000


it should be

#define mytmr1con *(volatile unsigned int *)0xbf800600


ie you have one too many zeroes - see page 44 of the PIC32MX1XX/2XX data document.

Peter
The only Konstant is Change
 
rogersan
Regular Member

Joined: 10/04/2015
Location: Australia
Posts: 79
Posted: 03:34am 12 May 2015
Copy link to clipboard 
Print this post

I am trying to use port number 0 and PIN number 0 so effectively Pin 2 of the 28 pin Micromite.

I am trying to use MMEdit to extract the program from the Micromite but I am finding it difficult to do, I will have to study the instructions.

My call to the function is
i=dht11convppm(port,pin,rawdata())

As the Cfunction is using pointers for port and pin, do I need to make port and pin in the "function call" to also be pointers? How can you do this in basic?
 
G8JCF

Guru

Joined: 15/05/2014
Location: United Kingdom
Posts: 676
Posted: 03:51am 12 May 2015
Copy link to clipboard 
Print this post

@RogerSan

  Quote  
I am trying to use MMEdit to extract the program from the Micromite but I am finding it difficult to do, I will have to study the instructions.


Just do List All in MMEdit Chat Window, highlight the text, then switch to MMEdit Edit Window, then do Ctrl+V to paste the text in.

But why ????

Could you please just post your MMBasic code ? See my earlier post, there is NO NEED TO MAKE POINTERS IN MMBASIC

Peter
The only Konstant is Change
 
rogersan
Regular Member

Joined: 10/04/2015
Location: Australia
Posts: 79
Posted: 03:52am 12 May 2015
Copy link to clipboard 
Print this post

Thanks for all the help, my CFunction machine code has changed considerably. As it is late here I will get back to it in the morning.
 
rogersan
Regular Member

Joined: 10/04/2015
Location: Australia
Posts: 79
Posted: 04:02am 12 May 2015
Copy link to clipboard 
Print this post

I need to learn how to get the program off the Micromite into MMEdit.

I have just been using TeraTrem but it is too limited for downloading and uploading.

Once I have read the instructions for MMEdit I hope to be able to do this. I am not sure whether to use
File/MMFileMaster or File/Chat to MaxiMite - it says Not Set Chat?
 
G8JCF

Guru

Joined: 15/05/2014
Location: United Kingdom
Posts: 676
Posted: 04:15am 12 May 2015
Copy link to clipboard 
Print this post

@RogerSan

In MMEdit, do Connect | New and pick the correct COM port and speed, usually 38400. Make sure Tera Term is disconnected.

Then in MMEdit, do File | Chat to MaxiMite

The MMEdit chat window should then be displayed.

In the thin text box, type List All <return>

Your MMBasic program should be displayed.

Now Select the text of your program, right click Copy, and then paste into the Edit window of MMEdit. DO FILE SAVE !!

Sleep well

Peter
The only Konstant is Change
 
rogersan
Regular Member

Joined: 10/04/2015
Location: Australia
Posts: 79
Posted: 12:34pm 12 May 2015
Copy link to clipboard 
Print this post

Thanks Peter.

Can you tell me how to download from MMEdit into the Micromite. Then I can do the editing in MMEdit.

Thank You
 
G8JCF

Guru

Joined: 15/05/2014
Location: United Kingdom
Posts: 676
Posted: 12:48pm 12 May 2015
Copy link to clipboard 
Print this post

@RogerSan

In MMEdit, there is a Blue Running Man holding a Flaming Torch at the far right hand side of the toolbar. When U click on the Blue Running Man, MMEdit will connect to the uMite and download your code.

Hopefully this question means that you are 99.999% of the way there !!

Pls get back and let us know how it goes

Peter
The only Konstant is Change
 
rogersan
Regular Member

Joined: 10/04/2015
Location: Australia
Posts: 79
Posted: 02:13pm 13 May 2015
Copy link to clipboard 
Print this post

I can now read the DHT22 using the DHT11 program by setting the data line on the Micromite to an input during the DHT11conv CFunction. I did have to put in a wait until the data line went high, after setting the pin to an input.

while((*myport&j)==0); //wait for data line to go high

I also found that the data calculation Is different for the DHT11 versus the DHT22

For the DHT11
Temp=th+tl/10 and Humidity=hh+hl/10

For the DHT22
Temp=thx256+tl. And Humidity=hhx256+hl

So to cater for both sensors, an extra variable in the call to sub dht11 would be required, to select the calculation required.
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 8601
Posted: 09:16pm 13 May 2015
Copy link to clipboard 
Print this post

  Quote  I can now read the DHT22 using the DHT11 program


@Rogersan - Well done

Your success in getting to grips with the whole Cfunction process will allow you to do much more in the future.

Peter
 
G8JCF

Guru

Joined: 15/05/2014
Location: United Kingdom
Posts: 676
Posted: 03:07am 14 May 2015
Copy link to clipboard 
Print this post

Hi Roger

May I just add my congratulations to Peter Mather's - very well done - the effort will pay off handsomely.

Per ardua ad astra !



Peter
The only Konstant is Change
 
rogersan
Regular Member

Joined: 10/04/2015
Location: Australia
Posts: 79
Posted: 08:00pm 16 May 2015
Copy link to clipboard 
Print this post

Thanks for your comments.

I am looking at reading the SHT2, a temperature and humidity sensor using I2C. I am looking at Peter Mather's code for the BMP80 which I want to use as a model.

I am having trouble understanding the string variable

Dim i2cin$ length 8

used to read in the data bytes from the BMP180. However the calibration data for the BMP180 of 22 bytes (11x 16 bits), is read into into i2cin$ which is only 8 characters. So how is this possible? Why doesn't i2cin$ have to be 22 characters?
 
rogersan
Regular Member

Joined: 10/04/2015
Location: Australia
Posts: 79
Posted: 08:01pm 16 May 2015
Copy link to clipboard 
Print this post

Sorry that should be the Sensirion SHT21.
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 8601
Posted: 09:33pm 16 May 2015
Copy link to clipboard 
Print this post

Looks like a bug that MMBasic is clever enough to correct. It should be 22 as you say. I can only assume that MMBasic uses the line
"I2C READ i2caddr,0,22,i2cin$() 'read in calibration data "
to re-dimension the array

As usual the BB has corrupted the C code as it uses variable i

correct version below:


unsigned long long intconv(unsigned char innumber[],unsigned int *signednum){
int k,j;
union utype{
unsigned long long b;
unsigned char a[8];
}u;
u.b=0;
j=innumber[0];//get the number of bytes
for(k=1;k<=innumber[0];k++)u.a[j-k] =innumber[k];
if (*signednum){k=u.a[j-1] & 0x80;//get the top bit
if(k){
for( ;j<=7;j++)u.a[j]=0xFF;//sign extend
}
}
return u.b;
}

unsigned long long powerof2(unsigned int *power){
int octet,place;
union utype{
unsigned long long b;
unsigned char a[8];
}u;
u.b=0;
octet=*power/8;
place=*power-(octet*8);
u.a[octet]=1<<place;
return u.b;
}
 
rogersan
Regular Member

Joined: 10/04/2015
Location: Australia
Posts: 79
Posted: 03:46pm 17 May 2015
Copy link to clipboard 
Print this post

For the C code a comment line at the top of the C code describing the function of the code, and the inputs, and the returned value would help.

For the intconv function, the input is a string, and an integer flagging the output of the function as signed or unsigned. The output is an integer equal the sum of the values of the hex characters in the input string. By the way I cannot see where u.b is altered from its initial value of 0.

For the powerof2 function, the input is the power of 2, and the output is the integer equal to that power. Again I cannot see where u.b is altered.
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 8601
Posted: 09:37pm 17 May 2015
Copy link to clipboard 
Print this post

  Quote  Again I cannot see where u.b is altered.


To read the code you have to understand the concept of a UNION


union utype{
unsigned long long b;
unsigned char a[8];
}u;


The char array "a[]" and the 64-bit integer "b" share the same bit of memory created by "u" so if I change something in "a[]" it also changes the value of "b".

I choose which one to address using the construct u.a[] or u.b.

intconv gets an integer binary number which happens to be stored in a string variable but whose length is less than 8 bytes and stretches it to fill a 64-bit integer. For a positive number this is easy but for a negative number the top bit must be written into all the higher bytes to fill the 64-bit space.
 
rogersan
Regular Member

Joined: 10/04/2015
Location: Australia
Posts: 79
Posted: 12:29am 18 May 2015
Copy link to clipboard 
Print this post

Normally each character of a string is the ASCII value of the character eg. '1' is 0x31. However when you read into a string using i2cread, a character in the string will have a value like 0xa8, an 8 bit integer.

So the intconv takes these "characters" and moves them to the right side of char a[8], then integer b which shares the same 64 bits, takes the value of the all the 8 bit integers inserted into a[8].
 
     Page 3 of 3    
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024